User Tools

Site Tools


aoe:bash

Bash things to remember

The comma operator is kind of cool

echo one{,two{,three}{,four}}five
onefive onetwofive onetwofourfive onetwothreefive onetwothreefourfive

Last command error

steve@steve-thinkpad:~$ echo $?
0
steve@steve-thinkpad:~$ blah
bash: blah: command not found
steve@steve-thinkpad:~$ echo $?
127

colon operator is a nop that returns true. Has many uses.

Parenthesis designate a command group

(a=hello; echo $a)

or provide array initialization.

Array=(element1 element2 element3)

A command may act upon a comma-separated list of file specs within braces.

echo \"{These,words,are,quoted}\"   # " prefix and suffix
# "These" "words" "are" "quoted"
  
cat {file1,file2,file3} > combined_file
# Concatenates the files file1, file2, and file3 into combined_file.

cp file22.{txt,backup}
# Copies "file22.txt" to "file22.backup"

\<, \>

word boundary in a regular expression.

bash$ grep '\<the\>' textfile
for a in charon titan; do ssh $a 'hostname;cat /proc/cpuinfo |grep -e "model name" -e "cpu MHz" |sort -r|uniq;cat /proc/meminfo |grep "MemTotal"|uniq'; done

&& as a error detector to halt further execution

cd /blah && echo $PWD
cd /etc && echo $PWD

error status

echo hello
echo $?    # Exit status 0 returned because command executed successfully.
lskdf      # Unrecognized command.
echo $?    # Non-zero exit status returned because command failed to execute.

http://www.cyberciti.biz/faq/bash-shell-read-a-line-field-by-field/

For each line I need to construct and execute a shell command as follows:

/path/to/deviceMaker --context=$1 -m $permissions $device2 $deviceType $major $minor

You can to use a while loop along with the read command, Internal Field Separator (IFS), and HERE STRINGS as follows:

#!/bin/bash
input=/path/to/data.txt
[ $# -eq 0 ] && { echo "Usage: $0 arg1"; exit 1; }
arg="$1"
cmd=/path/to/deviceMaker
while read -r line
do
	IFS=, read -r f1 f2 f3 f4 f5 <<<"$line"
        # quote fields if needed
	$cmd --context="$arg" -m $f5 $f1 $f2 $f3 $f4
done <"$input"

ping FOR loop network scan

These have a bug in them. It tries to scan 192.168.2.echo.

for i in `seq 1 254` ; do ping -c 1 -w 1 -n 192.168.2.$i > /dev/null && echo 192.168.2.$i ; done
for i in {1..254} ; do ping -c 1 -w 1 -n 192.168.2.$i > /dev/null && echo 192.168.2.$i ; done

Mac version

for i in {1..254} ; do ping -c 1 -W 10 -n 192.168.2.$i > /dev/null && echo 192.168.2.$i ; done
aoe/bash.txt · Last modified: 1970/01/01 00:00 (external edit)