Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Shell Scripting Cheat Sheet for Unix and Linux, Cheat Sheet of Computer Communication Systems

Shell Scripting Cheat Sheet for Unix and Linux: File Redirection, Common Constructs, Misc Useful Commands and Tools, and so on.

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

ekassh
ekassh 🇺🇸

4.7

(23)

274 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
v1.3 Sept
2012
Shell Scripting Cheat Sheet
for Unix and Linux
File Redirection Test Operators Variable Substitution
> file create (overwrite) file ${V:-default} $V, or “default” if unset
>> file append to file # do something ${V:=default} $V (set to “default” if unset)
< file read from file ${V:?err} $V, or “err” if unset
a | b Pipe 'a' as input to 'b'
Numeric Tests Conditional Execution
Common Constructs less than cmd1 || cmd2 run cmd1; if fails, run cmd2
$ while read f read text file greater than cmd1 && cmd2
> do line by line equal to
> echo “Line is $f” not equal Files
> done < file note: “$” prompt greater or equal
becomes “>” le less or equal ls a* list files beginning with “a”
find lines in ls *a list files ending with “a”
File Tests list oldest first, newest last
containing the newer than list smallest first, biggest last
d is a directory ls -a list all files, including hidde n
f is a file
x executable directory, preserving
Steve Parker delimited by colon r readable links, special devices, etc.
$ cmd1 || cmd2 run cmd1; if fails, writeable
run cmd2
$ cmd1 && cmd2 run cmd1; if it String Tests $SHELL what shell am I running?
works, run cmd2 = equal to $RANDOM provides rand om numbers
act upon the z zero length $$ PID of current process
a) value of a n not zero length $?
variable $!
b) Logical Tests
note that “;;” && logical AND Generally Useful Commands
*) is required || logical OR determine file type
at the end of ! logical NOT strip directory name (ls)
;; each section get directory name (/bin)
Arguments show all network adapters
get output of $0 program name show routers
ls into variable $1 show open ports
function $2 Year, Month, Day
declaration date +%H%M Hours, Mi nutes
} and syntax $# no. of arguments count number of lines
for calling it $* all arguments present working directory
Misc Useful Commands and Tools
find . -size 10k -print files over 10Kb
“bar” in file find text files
of each line less file display file page by page
March 1973
show disk mounts
simple maths trace system calls for PID
echo "scale = 5 ; \ better maths create tar archive
(5.00097) log in to host as user
touch file create blank file
alias ll='ls -l' alias for ls -l
unset existing alias return to previous directory
Online: http://steve-parker.org/sh/sh.shtml
Book: http://steve-parker.org/shellscripting
if [ “$x” -lt “$y” ]; then
fi
lt
gt run cmd1; if ok, run cmd2
eq
ne
ge mv /src /dest move /src into / dest
$ grep foo myfile
afoo myfile ls - ltr
foo n t ls -lSr
foobar text “foo”
find /src -print \ copy /src into current
$ cut -d: -f5 /etc/passwd get 5th field | cpio -pudv m
w
Preset Variables
case $foo in
return code from last cmd
echo “foo is A” ;; PID of last background cmd
echo “foo is B” ;;
file /etc/hosts
echo “foo is not A or B” basename /bin/ls
dirname /bin/ls
esac ifconfig -a
myvar=`ls` netstat -r
1st argument netstat -a
doubleit() { 2nd argument date +%Y%m%d
expr $1 \* 2
wc -l
doubleit 3 # returns 6 pwd
egrep “(foo|bar)” file find “foo” or
find . -name “*.txt” -print
awk '{ print $5 }' file print the 5th word find /foo -type d -ls list all directories under /foo
cal 3 1973 sed s/foo/bar/g file replace “foo” with “bar”
df -h sed -i s/foo/bar/g file in file (-i: update file)
three=`expr 1 + 2` strace -tfp PID
tar cvf archive.tar file1 file 2 f ile3
5121 / 1024" | bc ssh user@host
time cmd stopwatch on cmd scp file.txt user@host: copy file.txt to host as user
scp user@host:/tmp/file.txt /var/tmp copy /tmp/file.txt from user
at host to /var/tmp locally
unalias ls cd -

Partial preview of the text

Download Shell Scripting Cheat Sheet for Unix and Linux and more Cheat Sheet Computer Communication Systems in PDF only on Docsity!

v1.3 Sept

Shell Scripting Cheat Sheet

for Unix and Linux

File Redirection Test Operators Variable Substitution

file create (overwrite) file ${V:-default} $V, or “default” if unset

file append to file # do something ${V:=default} $V (set to “default” if unset) < file read from file ${V:?err} $V, or “err” if unset a | b Pipe 'a' as input to 'b'

Numeric Tests Conditional Execution

Common Constructs less than cmd1 || cmd2 run cmd1; if fails, run cmd

$ while read f read text file greater than cmd1 && cmd

do line by line equal to

> echo “Line is $f” not equal Files

done < file note: “$” prompt greater or equal becomes “>” le less or equal ls a* list files beginning with “a” find lines in ls *a list files ending with “a” File Tests list oldest first, newest last containing the newer than list smallest first, biggest last d is a directory ls -a list all files, including hidden f is a file x executable directory, preserving Steve Parker delimited by colon r readable links, special devices, etc. $ cmd1 || cmd2 run cmd1; if fails, writeable run cmd $ cmd1 && cmd2 run cmd1; if it String Tests $SHELL what shell am I running? works, run cmd2 = equal to $RANDOM provides random numbers act upon the z zero length $$ PID of current process a) value of a n not zero length $? variable $!

b) Logical Tests

note that “;;” && logical AND Generally Useful Commands

*) is required || logical OR determine file type at the end of! logical NOT strip directory name (ls) ;; each section get directory name (/bin)

Arguments show all network adapters

get output of $0 program name show routers ls into variable $1 show open ports function $2 Year, Month, Day declaration … … date +%H%M Hours, Minutes } and syntax $# no. of arguments count number of lines for calling it $* all arguments present working directory

Misc Useful Commands and Tools

find. -size 10k -print files over 10Kb “bar” in file find text files of each line less file display file page by page March 1973 show disk mounts simple maths trace system calls for PID echo "scale = 5 ; \ better maths create tar archive (5.00097) log in to host as user touch file create blank file alias ll='ls -l' alias for ls -l unset existing alias return to previous directory

Online: http://steve-parker.org/sh/sh.shtml

Book: http://steve-parker.org/shellscripting

if [ “$x” -lt “$y” ]; then fi lt gt run cmd1; if ok, run cmd eq ne ge mv /src /dest move /src into /dest $ grep foo myfile afoo myfile ls -ltr foo nt ls -lSr foobar text “foo” find /src -print \ copy /src into current $ cut -d: -f5 /etc/passwd get 5th^ field | cpio -pudvm w

Preset Variables

case $foo in return code from last cmd echo “foo is A” ;; PID of last background cmd echo “foo is B” ;; file /etc/hosts echo “foo is not A or B” basename /bin/ls dirname /bin/ls esac ifconfig -a myvar=ls netstat -r 1 st^ argument netstat -a doubleit() { 2 nd^ argument date +%Y%m%d expr $1 * 2 wc -l doubleit 3 # returns 6 pwd egrep “(foo|bar)” file find “foo” or find. -name “*.txt” -print awk '{ print $5 }' file print the 5th^ word find /foo -type d -ls list all directories under /foo cal 3 1973 sed s/foo/bar/g file replace “foo” with “bar” df -h sed -i s/foo/bar/g file in file (-i: update file) three=expr 1 + 2 strace -tfp PID tar cvf archive.tar file1 file 2 file 5121 / 1024" | bc ssh user@host time cmd stopwatch on cmd scp file.txt user@host: copy file.txt to host as user scp user@host:/tmp/file.txt /var/tmp copy /tmp/file.txt from user at host to /var/tmp locally unalias ls cd -