Learn shell here
Display: echo "$var"
- If
- C/JS like:
if (( $var == 0 )); then...; fi
- Normal:
if [[ $var -eq $var2 ]]; then...; fi
- Is it in?:
if [[ $VAR == *"Linux"* ]]
if [[ $VAR =~ .*Linux.* ]]
- C/JS like:
- Switch:
case ... in
...) ... ;;
esac
- Is a string empty?:
-z
- Everything else is the same as other language using
(( ... ))
- For:
for; do...; done
- C/JS like:
for (( i = 0; i < 10; i++))
- Python like:
for i in {0..2}
- While: running as long as condition is true
while ... option; do ...; done
- Until: running as long as condition is false
until ...; do ...; done
- Declare it:
a=()
or initialized ita=(1 2 5 6 8 67)
- Get value at an index:
${a[0]}
- Get length:
${#a[@]}
- Add/get a value in following index in a loop:
tmp[${#tmp[@]}]=
- Get all values:
${z[*]}
or${z[@]}
(can be used in loop to retrieve values not index) - Copy of array:
z=("${tmp[@]}")
- Calculate:
$((i + 1))
- Declare a function:
function function_A {...}
- Get parameters & call a function is the same is calling a script and passing args
- Get parameters: 1rst param
$1
, 2nd param$2
, ... - call function:
function_A arg1 arg2
- Get parameters: 1rst param
see basics/string_basic_operation.sh
$0
: filename$n
: Nth arg passed was invoked or function was called
test it: if [ "$1" != "" ]$#
: nbr of arg passed (script or function)
there are arguments if > 0$@
or$*
: all args passed (script or function)
can loop on it$?
: exit status from last command executed$$
: process ID of shell (shell scripts: process ID under which they are executing)$!
: process nbr of last bg command
Catch special signal/interruption/user input in script to prevent the unpredictables trap <arg/function> <signal>
ex: trap "echo Booh!" SIGINT SIGTERM
--> on Crtl+C displays Booh!
- File exists?:
if [ -e "$filename" ]
- Directory exists?:
if [ -d "$directory_name" ]
- Read permission?:
if [ ! -f "$filename" ]
- To get arguments see Special Variables section
- Read option values: loop on
getops
function- ex: get value for
-u -a -l
while getopts u:a:l: option
do
case "${option}" in
u) USER=${OPTARG};;
a) AGE=${OPTARG};;
l) LOCATION=${OPTARG};;
esac
done
- ex: get value for
- Counts nbr entries in directory:
ls / | wc -l
- Get 1rst 10 results:
ls / | head
outputs the first 10 lines by default, use option -n to change
Remark: By default pipelines redirects only the standard output, if you want to include the standard error you need to use the form |& which is a short hand for 2>&1 |
- Save sorted results to new files & show diffs between files:
- without process subs:
sort file1 > sorted_file1
sort file2 > sorted_file2
diff sorted_file1 sorted_file2
- with:
diff <(sort file1) <(sort file2)
- without process subs:
- Store logs app into file & print it in console:
echo "Hello, world!" | tee /tmp/hello.txt
- Only lower case in file but keep regular case on output:
echo "Hello, world!" | tee >(tr '[:upper:]' '[:lower:]' > /tmp/hello.txt)