Skip to content

Instantly share code, notes, and snippets.

@terryoy
Last active December 11, 2015 00:39
Show Gist options
  • Save terryoy/4518272 to your computer and use it in GitHub Desktop.
Save terryoy/4518272 to your computer and use it in GitHub Desktop.
Linux Shell Programming
### IO redirecting ###
# "2" is stderr
kill -HUP >killout 2>killerr.txt
# add stderr to the same file as stdout
kill -l 1234 >killout 2>&1
# output to "Recycle Bin"
kill -l 1234 >/dev/null 2>&1
# redirect stdin
more < killout.txt
### Pipe ###
# output all the process running in the system
ps xo comm | sort | uniq | grep -v sh | more
# the outupt file will be created the same time as the command was created, so you will get an empty input file if use the same file as input and output
cat mydata.txt | sort | uniq | >mydata.txt
### wildcards(通配符) in shell scripts ###
# * - anything
# ? - match one character
# [set] [^set] - containing or excluding the character listed
# {apple,banana} - including whole word
ls my_{finger,toes}s
### managing a collection of searched files ###
# example 1: using "for" loop
$ for file in *
> do
> if grep -l POSIX $file
> then
> more $file
> fi
>done
# example 2: using "''"
more 'grep -l POSIX *'
# example 3: using"$()"
more $(grep -l POSIX *)
# example 4: using pipe
grep -l POSIX * | more
### writing scripts ###
# check file type
file afile
# the content of a script:
# #!/bin/sh
# # do sth...
# exit 0
# setting values to variables
$ salute=Hello
$ echo $salute
Hello
$ salute="Hello dear"
$ echo $salute
Hello dear
$ salute=7+5
$ echo $salute
7+5
=== Grammar ===
# echo $myvar - the value of the variable $myvar
# echo "$myvar" - string(not the variable)
# echo \$myvar - same as above
--- if clause ---
# if condition
# then
# statements
# elif condition; then
# statements
# else
# statements
# fi
---for clause ---
# for var in foo bar 43 # if you use "" to quote, it would become a string instead of collection, e.g. "foo bar 43"
# do
# echo $var
# done
---while clause ---
# while [condition]; do
# echo sth
# done
--- until clause ---
# until [condition]; do
# echo sth
# done
--- case clause ---
# case variable in
# foo1) statements;;
# 2) statements;;
# *) statements;;
# esac
--- AND command sequence: only when the first command return successful then execute the next one ---
# statement1 && statement2 && statement3 && ...
--- OR command sequence: execute all commands until one return successful ---
# statement1 || statement2 || statement3 || ...
--- code block ---
# statement1 && {
# statements
# }
--- functions ---
# function foo() {
# local localVar;
# statements
# }
# # execution:
# foo
--- special commands ---
# : - represent a true, e.g. "while :"
# break - break a loop
# continue - skip the rest part and go to the next loop
# . - usually when you run a shell script it will create and run in a new environment, this command is to require running in the same env as the current one. e.g. ". ./shell_script"
# echo -n "string" - echo without endline
# echo -e "string\n" - echo with escape characters, which isn't in default. e.g. it will output two endline in this case, one for echo, one for "\n"
# eval $foo - evaluate the value of the value in the parameter. e.g. if foo = '$bar', this line will print the value of $bar
# exec - there are several usages:
# 1. replace the current shell(self) with a command without creating a new process, it will end the current shell if the program has error or quit(e.g. "exec echo 123")
# 2. redirect STDIN or STDOUT(e.g. "echo 1>>$LOG", "echo 2>>&1")
# 3. open files as file descriptor(n). (e.g. "exec 3<inputfile", "exec 4>outputfile", "exec 5<&0"(standard input), ...)
# exit n - exit shell with code n, if not specified, use the last command's return code
# export - export variables as ENV variables globally to all other scripts/program
# expr - calculate fomula (e.g. x=`expr $y+1`, x=$(expr $y+1) )
# printf "formated string" param1 param2 - format print
# return - end of a function
# set - set env variables
# shift - shift left all the parameter variables(e.g. $1 -> $0, $2 -> $1, ..)
# trap <command> <signal>- signal call back, use "trap -l" to list all available signals(HUP:hang, INT:^C, QUIT, ABRT, ALRM, TERM,...)
# unset - unset variables
# find - find files
# grep - search in files(e.g. "grep e$ words.txt" - find lines end with "e")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment