


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This file contains short tables of commonly used items in this shell. In most cases the information applies to both the Bourne shell (sh) and the newer bash shell.
Typology: Cheat Sheet
1 / 4
This page cannot be seen from the preview
Don't miss anything!
-r file Check if file is readable. -w file Check if file is writable. -x file Check if we have execute access to file. -f file Check if file is an ordinary file (as opposed to a directory, a device special file, etc.) -s file Check if file has size greater than 0. -d file Check if file is a directory. -e file Check if file exists. Is true even if file is a directory.
if [ -s file ] then #such and such fi
s1 = s2 Check if s1 equals s2. s1 != s2 Check if s1 is not equal to s2. -z s1 Check if s1 has size 0. -n s1 Check if s2 has nonzero size. s1 Check if s1 is not the empty string.
if [ $myvar = "hello" ] ; then echo "We have a match" fi
n1 -eq n2 Check to see if n1 equals n2. n1 -ne n2 Check to see if n1 is not equal to n2. n1 -lt n2 Check to see if n1 < n2. n1 -le n2 Check to see if n1 <= n2. n1 -gt n2 Check to see if n1 > n2. n1 -ge n2 Check to see if n1 >= n2.
if [ $# -gt 1 ] then echo "ERROR: should have 0 or 1 command-line parameters" fi
! not -a and -o or
if [ $num -lt 10 -o $num -gt 100 ] then echo "Number $num is out of range" elif [! -w $filename ]
then echo "Cannot write to $filename" fi
if [ $myvar = "y" ] then echo "Enter count of number of items" read num if [ $num -le 0 ] then echo "Invalid count of $num was given" else #... do whatever ... fi fi
if grep -q shell bshellref then echo "true" else echo "false" fi
pgm > file Output of pgm is redirected to file. pgm < file Program pgm reads its input from file. pgm >> file Output of pgm is appended to file. pgm1 | pgm2 Output of pgm1 is piped into pgm2 as the input to pgm2. n > file Output from stream with descriptor n redirected to file. n >> file Output from stream with descriptor n appended to file. n >& m Merge output from stream n with stream m. n <& m Merge input from stream n with stream m. << tag Standard input comes from here through next tag at start of line.
$0 Name of this shell script itself. $1 Value of first command line parameter (similarly $2, $3, etc) $# In a shell script, the number of command line parameters. $* All of the command line parameters. $- Options given to the shell. $? Return the exit status of the last command. $$ Process id of script (really id of the shell running the script)
\c Take character c literally. cmd
Run cmd and replace it in the line of code with its output. "whatever" Take whatever literally, after first interpreting $, ...
,
'whatever' Take whatever absolutely literally.
result= expr** $1 + 2 **
result2= expr** $2 + $1 **/** 2 **
result= expr** $2 \ ***** 5 **
#note the \ on the * symbol
result=$ [ $1 + 3 ]
brace expansion (see a reference book) ~ expansion (for login ids) parameters (such as $1) variables (such as $var) command substitution (Example: match=grep DNS *
) arithmetic (from left to right) word splitting pathname expansion (using *, ?, and [abc] )
$var Value of shell variable var. ${var}abc Example: value of shell variable var with string abc appended.
var=value Assign the string value to shell variable var. cmd1 && cmd2 Run cmd1, then if cmd1 successful run cmd2, otherwise skip. cmd1 || cmd2 Run cmd1, then if cmd1 not successful run cmd2, otherwise skip. cmd1; cmd2 Do cmd1 and then cmd2. cmd1 & cmd2 Do cmd1, start cmd2 without waiting for cmd1 to finish. (cmds) Run cmds (commands) in a subshell.