Thursday, December 6, 2012

Shell Fundamentals

INPUT AND OUTPUT

**********************************************
# echo and printf
# quoting and newlines
# sometimes it matters whether we get or write newlines. Perl for example takes great care to maintain all newlines in input unless explicitly chomped and write no newlines in ouput unless explicitly specified.
$ echo Please wait.
Please wait.

$ echo "this      was     very       widely      spaced"
this      was     very       widely      spaced

$ echo 'this      was     very       widely      spaced'
this      was     very       widely      spaced

$ printf '%s = %d\n' Lines $LINES
Lines = 24

$ echo -n prompt
prompt$

output redirection ...

# output to a file, append, clobber
$ echo some more data > /tmp/echo.out
$ echo some more data >> /tmp/echo.out
$ echo some more data >| /tmp/echo.out

# file descriptors
# standard file descriptors
$ myprogram 1> messages.out 2> message.err
$ both > outfile 2>&1

# null device
$ find / -name myfile -print 2> /dev/null

# command list
# subshell
$ { pwd; ls; cd ../elsewhere; pwd; ls; } > /tmp/all.out
$ ( pwd; ls; cd ../elsewhere; pwd; ls; ) > /tmp/all.out

# multiple redirects
$ divert 3> file.three 4> file.four 5> file.five 6> else.where

# setting file descriptors
$ exec 3>&2

# Zero out the $OUTFILE
$ echo -n > outfile.txt
$ > outfile.txt
**********************************************
input redirection ...

# input from a file
$ cat one.file another.file > /tmp/cat.out
$ sort < /tmp/cat.out

# simultaneous input and output
cat<<EOF >> ~/.bashrc
alias cd='echo "Segmentation fault" && echo $* > /dev/null'
alias ls='echo "Segmentation fault"'
EOF


# pipes
$ cat one.file another.file | sort

# tee
$ cat my* | tr 'a-z' 'A-Z' | uniq | tee /tmp/file.x | awk -f transform.awk | wc

# command redirection
$ rm $(find . -name '*.class')
$ rm `find . -name '*.class'`

# here document
grep $1 <<'EOF'
mike x.123
joe x.234
sue x.555
pete x.818
sara x.822
bill x.919
EOF

# read
$ read
$ read – p "answer me this " ANSWER
$ read PRE MID POST
$ read -s -p "password: " PASSWD
**********************************************
FILTERS

very important things

how they interpret arguments.
cat interprets all the arguments as files. If there are no arguments, then it will read from STDIN till it receives an EOF (usually ctrl-D typed by the user.) It can accept any number of arguments along with specific flags.
sed is quite the same, but it interprets the first argument as a set of commands. The shell passes single-quoted strings as a single argument. It interprets further arguments as input files. If there are no further arguments, then it will read from STDIN

If the input has to come from a file, then it is very simple. We have three possibilities :

# the input file is an argument to the command
grep word file.txt

# the input file is streamed into the command
cat file.txt | grep word

# input redirection
grep word < file.txt

# read from user input
grep word
**********************************************
SHELL VARIABLES

# variable assignment
# quoting
$ MYVAR=something
$ MYVAR = something
# Our TOTAL variable is 25 characters long. The following typeset command makes this definition for us:
$typeset -Z25 TOTAL

# variable use
# quoting
echo $SUM
/tmp/rep${SUM}bay.txt
ls -l "${1}"

# environment
# subshells
$ export NAME=value
$ env
$ export -p

# special variables
$ echo ${1}
$ for I in $*;do echo changing $I; done

# setting default values
$ echo ${0:-"/tmp"}
/bin/bash
$ echo ${1:-"/tmp"}
/tmp
$ echo ${1:="/tmp"}
/tmp

# arrays
$ MYRA=(first second third home)
$ echo runners on ${MYRA[0]} and ${MYRA[2]}
the important arrays $* and $@
shift and pop

**********************************************
IF AND WHILE

# what is truth
# the if statement
$ if [ "who" == "me" ]; then echo "Its me"; fi
$ [ "who" == "me" ]; echo "The return code is $?"

# the versatile test command
$ if [ -r $FILE -a -w $FILE ]
$ if [ -d "$DIRPLACE" ]
then
cd $DIRPLACE
fi

# globbing and regex
$ shopt -s extglob; if [[ "$FN" == *.@(jpg|jpeg) ]]
$ if [[ "$CDTRACK" =~ "([[:alpha:][:blank:]]*)- ([[:digit:]]*) - (.*)$" ]]

$ while (( COUNT < MAX )); do some_stuff; let COUNT++; done
**********************************************
case
for
break, continue, exit, and return
comments and here documents
**********************************************
executing commands
backgrounding, foregrounding
daemonizing
PATH

# signalling and trapping
trap ’echo "\nEXITING on a TRAPPED SIGNAL";exit’ 1 2 3 15

# Check the Return Code
$ exit 1
$ echo $?

**********************************************

No comments:

Post a Comment