Bash Script Cheat Sheet



Comments



Description

http://steve-parker.org/sh/sh.shtml v1.1 – 7 Aug 2007 UNIX / Linux Shell Cheat Sheet File Manipulation > file >> file >file 2>&1 < file a|b create (overwrite) file append to file both output and errors to file read from file pipe output from 'a' as input to 'b' Test Operators if [ “$x” -lt “$y” ]; then # do something fi Numeric Tests lt gt read text file line by line eq ne ge le less than greater than equal to not equal greater or equal less or equal Variable Substitution ${V:-def} ${V:=def} ${V:?err} $V, or “def” if unset $V (set to “def” if unset) $V, or “err” if unset Conditional Execution c1 || c2 c1 && c2 run c1; if it fails, run c2 run c1; if it works, run c2 Common Constructs while read f do echo “Line is $f” done < file $ grep foo myfile afoo foo foobar $ cut -d: -f5 /etc/passwd Dilbert foo=`ls` Common utilities and switches ls -lSr ls -ltr ls -lh du -sk * sort -n ps -ef wget URL time cmd touch file read x cmd | \ tee file.txt nice cmd list files, biggest last list files, newest last human-readable filesizes directory sizes (slow) sort numerically (not alpha) list my commands download URL stopwatch on `cmd` create file read “x” from keyboard cmd output to stdout and also to file.txt run cmd with low priority find matching lines get field with delimiter get output of command File Tests nt newer than d f r w x is a directory is a file readable writeable executable String Tests = equal to z n zero length not zero length case $foo in a) echo “foo is A” ;; b) echo “foo is B” ;; *) echo “foo is not A or B” ;; esac doubleit() { expr $1 \* 2 } doubleit 3 # returns 6 function declaration and calling syntax A for loop for i in * do echo “File is $i” done iterates through its input (which is subject to globbing) case is a good way to avoid iterating through many if/elif/elif/elif constructs. Networking ifconfig -a netstat -r ssh u@host scp file.txt \ u@host: list all network interfaces show routers log in to host as user “u” copy file.txt to host as user “u” Logical Tests && || ! logical AND logical OR logical NOT Argument Variables $0 $1 $2 ... $9 $* $# program name 1st argument 2nd argument ... 9th argument all arguments No. of arguments General Admin less file alias l='ls -l' tar cf t.tar \ list_of_files cal 3 1973 df -h truss -p PID display file, page by page create “l” as alias for “ls -l” create a tar archive t.tar from the listed dirs/files display a calendar (Mar 73) show disk mounts show syscalls of PID Files: Contents / Attributes find . -size 10k -print find . -name “*.txt” -print find /foo -type d -ls three=`expr 1 + 2` echo "scale = 5 ; 5121 / 1024” | bc egrep “(foo|bar)” file files over 10Kb find text files ls all directories under /foo simple maths better maths find “foo” or “bar” in file print the 5th word of each line replace “foo” in file with “bar” Useful Variables $IFS $? $SHELL LANG Internal File Separator return code from last program what shell is running this script? Language; C is US English awk '{ print $5 }' file sed s/foo/bar/g file A full PDF and online tutorial is available at http://steve-parker.org/sh/sh.shtml
Copyright © 2024 DOKUMEN.SITE Inc.