Various 2
readonly readonly x=4 x=44 #this will give an error (like what in java?)
talking to myself echo echo talking to myself > newScript.sh sh newScript.sh rm newScript.sh #what are the potential problems with this? #how can it be improved?
Command substitution the shell can insert standard output of a command at any point in the command line. There are two ways to do this `…` backquotes $(…)
Example - Command substitution echo date is `date` Notice the backquotes! echo date is $(date) d=$(date) echo date is $d ~]$ d=`date` ~]$ echo $d Tue Apr 7 15:52:35 CST 2009
$(…) or `…` $() is better that `` easier to read! (think of if in java) can nest easier $($(...)) note can nest with backquotes too
quotes '...' removes meaning of all enclosed characters "..." removes meaning of all enclosed characters except $, ` and \ \c removes meaning of special character c ($, ', " newline, and \) `command` or $(command) executes the command (\ is also used for continuing new lines)
A unix philosophy If you have to do it more than twice – write a script to do it. This will take time at the beginning – but you will become faster. The overhead is worth it. If you have to do it more than twice – so has someone else – so you can google for the code
What is the error here? for i in do echo i done
No Error!! It may be what the person wanted. You need to explain what you require. You can write what you want the expected output to be. Or explain.
\ for continuing new lines $ ls | wc Or if it is a long command I can continue it on another line ls \ ? | wc # a better example would be with awk or grep.
PS2 is the prompt here $ PS2=p2 $ echo $PS2 p2 $ ls \ p2 | wc $ Note – this is useful for long chains of commands.
Debugging (tracing) sh -x prog.sh or can do as set -x or can switch on and off in the script turn off with set +x (note that this is counter intuitive!!!) set with no arguments shows the variables. Printing is a good idea to confirm the values of variables, and isolate problems.
Setting a variable to null var= var="" var='‘ #two single quotes. var=`` #two back quotes are these all the same? If not – what are the error messages. Try it out for yourself.
Single and Double Quotes Single quote protect everything echo '$(ls)’ #will print...what? Double quotes filelist=$(ls) echo $filelist echo "$filelist" good for storing contents of a file namelist=$(cat names.txt) (we can do text processing on this)
expr expr expr 1 * 2 (this will give an error – what error?) expr 1 \* 2
${parameter} vs $parameter mv $file $fileBackUp (we want to append the filename) mv ${file} ${file}x Also for arguments to a script. This can also be used for ${1} cannot say $11 need ${11}
Null command : if true then : #we have to put something here else echo false fi #this is good when writing programs.
(…) and {…;} (..) this works in a subshell {…;} works in the current shell x=10 (x=40) echo $x # what will this print? {x=70;} echo $x #what will this print? #see subshell.sh on shared drive.
(…) example (command1; command 2 ) & This will do command1, then command2, but the parent shell can continue working. This is not the same as (command1 &; command2 & ) & command1; command 2 & Why not? What do these do?
{…} example {command1; command2; } 2> errors.txt Here the error output of the two commands is sent to the file errors.txt Is this the same as command1 2> errors.txt; command2 2> errors.txt { sh thisFileDoesNotExist.sh ; sh OrThisOne.sh ;} 2> errors.txt #see share groupingCommands.sh
break for i in do echo $i [ $i -eq 2 ] && break Done #break.sh on share
Break n #this will break out of the loop for i in do echo $i [ $i -eq 2 ] && break $n done This will break out of the n innermost loops #breakN.sh on share
continue #this will continue with the the loop for i in do echo before $i [ $i -eq 2 ] && echo skip && continue echo after $i done
Continue n Just like break has a break n Continue has a continue n See continue.sh and continueN.sh on share.
Loop redirection You can perform redirection on a loop for ((i = 1; i < 5; i++)) do echo $i done > out.txt #on share at loopRedirection.sh
Loop in background #this will execute a loop in the background. for ((i = 1; i < 50000; i++)) do echo $i >> out.txt done &
loopPiping #the output of the loop is piped into wc for ((i = 1; i < 5; i++)) do echo $i done | wc #see loopPiping.sh on share.
Forcing output 1 echo hello This prints to the screen (tellytype) by default OR whereever you direct it echo hello > out.txt
Forcing output 2 #If we want to force the output to the screen in a script #try to run this program as #sh devtty.sh > out.txt echo this will always go to the screen > /dev/tty
Be careful with rm (rm -i) $ ls a.txt b.txt c.txt remove.sh remove.sh~ $ rm a.txt rm: remove regular empty file `a.txt'? y $ sh remove.sh $ ls remove.sh remove.sh~