Posts

Showing posts from September, 2015

ch5 condition execution

spaces around the [ and ] are required echo $? return non-zero for failure and zero for success --------- #!/bin/bash # Test for failure cat “$1” if [ “$?” -ne “0” ]; then echo “Error: Reading $1 failed.” fi --------- -r test to see if a file exists and is readable #!/bin/bash # Test for likely causes of failure if [ ! -r “$1” ]; then echo “Error: $1 is not a readable file.” echo “Quitting.” exit 1 fi cat "$1" ---------- if [ -r “$1” ]; then cat “$1”; fi if [ ! -r “$1” ]; then echo “File $1 is not readable – skipping. “; fi ---------- In the shell, [ is a program, and ] is simply an argument to it, which is required, but discarded. It has no significance to the shell whatsoever. ---------- The ability to execute a directory is treated as the right to change into that directory ---------- This adds ~/bin to the PATH environment, but only if that directory exists: if [ -d ~/bin ]; then PATH=$PATH:~/bin fi ---------- cat rwx #!/bin/bash whi...

ch4 wildcards

Many (although not all) commands interpret two dashes (-- ) as indicating the end of the set of flags to the command itself, and the start of the list of filenames to operate on. So rm -- “-rf .“ will remove the file “-rf ” ----------- [a-z] is equivalent to [[:alpha:]], and [0-9] is equivalent to [[:digit:]] Class Members Alnum A-Z, a-z, 0-9 Alpha A-Z, a-z Digit 0-9 Upper A-Z The bash shell does offer more powerful wildcard explansion than the standard Bourne shell ----------- Sometimes you might want to disable filename expansion completely; the bash directive set -o noglob (alternatively set -f) disables it, and set +o noglob (or set +f) re-enables it. $ touch *d*.odt ----------- Setting the failglob option means that the shell itself will treat the use of a nonmatching expression as a shell error $ shopt failglob failglob off $ ls b* ls: cannot access b*: No such file or directory ----------- sed has the -i option which updates the file in place sed s...

shell scripting notes ch3 variables alias BASH_COMMAND BASH_SOURCE

From security perspective, it's very bad practice to put a dot (.) in your PATH, especially at the front of the PATH. You can set an alias in your ~/.bashrc file alias less="less -X" the command unalias less  removes this aliasing to bypass an alias - is to put a backslash before the command \rm - will call the analiased rm command HISTIGNORE is a colon-separated list of commands that shouldn't be stored in the history if you need to go via a proxy server, the ~/.wgetrc file can be used to set proxy settings for the wget tool. In the shell there is no concept of a "type", you could say that everything is a string. There is no need to explicitly declare the variable before using it. command substitution: VAR=`date`, VAR=$(date) no spaces are permitted around the equal sign Bourne shell doesn't allow VAR=$(date) syntax echo -n - tells echo not to put a newline character at the end  You cannot change the values of variables $0...

dollar variables

$1 ,  $2 ,  $3 , ... are the  positional parameters . "$@"  is an array-like construct of all positional parameters,  {$1, $2, $3 ...} . "$*"  is the IFS expansion of all positional parameters,  $1 $2 $3 ... . $#  is the number of positional parameters. $-  current options set for the shell. $$  pid of the current shell (not subshell). $_  most recent parameter (or the abs path of the command to start the current shell immediately after startup). $IFS  is the (input) field separator. $?  is the most recent foreground pipeline exit status. $!  is the PID of the most recent background command. $0  is the name of the shell or shell script.