Looping through the content of a file in Bash

Suppose you have this file:

$ cat /tmp/test.txt
Line 1
    Line 2 has leading space
Line 3 followed by blank line

Line 5 (follows a blank line) and has trailing space    
Line 6 has no ending CR

There are four elements that will alter the meaning of the file output read by many Bash solutions:

  1. The blank line 4;
  2. Leading or trailing spaces on two lines;
  3. Maintaining the meaning of individual lines (i.e., each line is a record);
  4. The line 6 not terminated with a CR.

If you want the text file line by line including blank lines and terminating lines without CR, you must use a while loop and you must have an alternate test for the final line.

Here are the methods that may change the file (in comparison to what cat returns):

1) Lose the last line and leading and trailing spaces:

$ while read -r p; do printf "%s\n" "'$p'"; done </tmp/test.txt
'Line 1'
'Line 2 has leading space'
'Line 3 followed by blank line'
''
'Line 5 (follows a blank line) and has trailing space'

(If you do while IFS= read -r p; do printf "%s\n" "'$p'"; done </tmp/test.txt instead, you preserve the leading and trailing spaces but still lose the last line if it is not terminated with CR)

2) Using process substitution with cat will reads the entire file in one gulp and loses the meaning of individual lines:

$ for p in "$(cat /tmp/test.txt)"; do printf "%s\n" "'$p'"; done
'Line 1
    Line 2 has leading space
Line 3 followed by blank line

Line 5 (follows a blank line) and has trailing space    
Line 6 has no ending CR'

(If you remove the " from $(cat /tmp/test.txt) you read the file word by word rather than one gulp. Also probably not what is intended…)


The most robust and simplest way to read a file line-by-line and preserve all spacing is:

$ while IFS= read -r line || [[ -n $line ]]; do printf "'%s'\n" "$line"; done </tmp/test.txt
'Line 1'
'    Line 2 has leading space'
'Line 3 followed by blank line'
''
'Line 5 (follows a blank line) and has trailing space    '
'Line 6 has no ending CR'

If you want to strip leading and trading spaces, remove the IFS= part:

$ while read -r line || [[ -n $line ]]; do printf "'%s'\n" "$line"; done </tmp/test.txt
'Line 1'
'Line 2 has leading space'
'Line 3 followed by blank line'
''
'Line 5 (follows a blank line) and has trailing space'
'Line 6 has no ending CR'

(A text file without a terminating \n, while fairly common, is considered broken under POSIX. If you can count on the trailing \n you do not need || [[ -n $line ]] in the while loop.)

More at the BASH FAQ

SSH Tunnels

Bash script found here: https://gist.github.com/scy/6781836

  • -fRun in the background before command execution.
  • -NDon’t execute any commands
  • -TDisable pseudo-tty allocation.
  • -S socketnameUse a control socket with name socketname
  • -MPut control socket in master mode
  • -O check, exitControl command
#!/bin/bash

ip="1.1.1.1"

socket=$(mktemp -t deploy-ssh-socket)
rm ${socket} # delete socket file so path can be used by ssh

exit_code=0

cleanup () {
    # Stop SSH port forwarding process, this function may be
    # called twice, so only terminate port forwarding if the
    # socket still exists
    if [ -S ${socket} ]; then
        echo
        echo "Sending exit signal to SSH process"
        ssh -S ${socket} -O exit root@${ip}
    fi
    exit $exit_code
}

trap cleanup EXIT ERR INT TERM

# Start SSH port forwarding process for mariadb (3306) and postgresql (5432)
ssh -M -S ${socket} -fNT -L 3306:localhost:3306 -L 5432:localhost:5432 root@${ip}

ssh -S ${socket} -O check root@${ip}

# launching a shell here causes the script to not exit and allows you
# to keep the forwarding running for as long as you want.
# I also like to customise the prompt to indicate that this isn't a normal shell.

bash --rcfile <(echo 'PS1="\nwith-ports> "')

ssacli – PhoenixWiki

ssacli

HP Enterprise ssacli command examples

Install ssacli – https://downloads.linux.hpe.com/SDR/project/mcp/

# Show configuration

ssacli ctrl all show config

# Controller status

ssacli ctrl all show status

# Show detailed controller information for all controllers

ssacli ctrl all show detail

# Show detailed controller information for controller in slot 0

ssacli ctrl slot=0 show detail

# Rescan for New Devices

ssacli rescan

# Physical disk status

ssacli ctrl slot=0 pd all show status

# Show detailed physical disk information

ssacli ctrl slot=0 pd all show detail

# Logical disk status

ssacli ctrl slot=0 ld all show status

# View Detailed Logical Drive Status

ssacli ctrl slot=0 ld 2 show

# Create New RAID 0 Logical Drive

ssacli ctrl slot=0 create type=ld drives=1I:1:2 raid=0

# Create New RAID 1 Logical Drive

ssacli ctrl slot=0 create type=ld drives=1I:1:1,1I:1:2 raid=1

# Create New RAID 5 Logical Drive

ssacli ctrl slot=0 create type=ld drives=1I:1:1,1I:1:2,2I:1:6,2I:1:7,2I:1:8 raid=5

# Delete Logical Drive

ssacli ctrl slot=0 ld 2 delete

# Add New Physical Drive to Logical Volume

ssacli ctrl slot=0 ld 2 add drives=2I:1:6,2I:1:7

# Add Spare Disks

ssacli ctrl slot=0 array all add spares=2I:1:6,2I:1:7

# Enable Drive Write Cache

ssacli ctrl slot=0 modify dwc=enable

# Disable Drive Write Cache

ssacli ctrl slot=0 modify dwc=disable

# Erase Physical Drive

ssacli ctrl slot=0 pd 2I:1:6 modify erase

# Turn on Blink Physical Disk LED

ssacli ctrl slot=0 ld 2 modify led=on

# Turn off Blink Physical Disk LED

ssacli ctrl slot=0 ld 2 modify led=off

# Modify smart array cache read and write ratio (cacheratio=readratio/writeratio)

ssacli ctrl slot=0 modify cacheratio=100/0

# Enable smart array write cache when no battery is present (No-Battery Write Cache option)

ssacli ctrl slot=0 modify nbwc=enable

# Disable smart array cache for certain Logical Volume

ssacli ctrl slot=0 logicaldrive 1 modify arrayaccelerator=disable

# Enable smart array cache for certain Logical Volume

ssacli ctrl slot=0 logicaldrive 1 modify arrayaccelerator=enable

# Enable SSD Smart Path

ssacli ctrl slot=0 array a modify ssdsmartpath=enable

# Disable SSD Smart Path

ssacli ctrl slot=0 array a modify ssdsmartpath=disable

Phoenix’s Wiki

Source: ssacli – PhoenixWiki

Bad Behavior has blocked 42 access attempts in the last 7 days.