#!/usr/bin/env bash
for portability, or less good,
#!/bin/bash
# this symbol makes everything after it on a line a comment
$
in front.local
before creating it.$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)
> filename
redirects stdout to a file (and creates
the file if it didn’t already exist)>> filename
redirects stdout to a file but it
appends to the end of the file if it already exists|
is the pipe. Piping is used to chain
commands, scripts, files and programs together.cat *.txt | sort | uniq > result-file
Sorts
the output of all the .txt files and deletes duplicate lines, saving
outcome to result-fileread
command e.g. read num
asks for input
and puts it in $numexpr
commandresult=`expr $1 + 2`
result2=`expr $2 + $1 / 2`
result=`expr $2 \* 5` #note the \ on the * symbol
if [ $# -gt 1 ]
&&
is logical and, ||
is
logical or! #not
-a #and
-o #or
-eq == equal to
-ne ≠ (not equal to)
-gt > greater than
-ge >= greather than or equal to
-lt < less than
-le <= less than or equal to
< less than
<= less than or equal to ((within double parenthesis))
> greater than ((within double parenthesis))
>= greater than or equal to ((within double parenthesis))
= equal to, including for strings, e.g. ```if [ "$a" = "$b" ]```
== is equal to
!= is NOT equal to
< less than (ascii-betically)
> greater than (ascii-betically)
-z string is null
-n string is not null
if [ "$VAR1" = "$VAR2" ]; then
echo "expression evaluated as true"
else
echo "expression evaluated as false"
fi
case "$C" in
"1")
do_this()
;;
"2" | "3")
do_what_you_are_supposed_to_do()
;;
*) #fallback default case
do_nothing()
;;
esac
### FOR loop
for i in 1 2 3 4 5 # can also be written for i in {1..5} or {start..end..increment}
do
echo "Welcome $i times"
done
select
looping works just like for
#!/bin/bash
OPTIONS="Hello Quit"
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]; then
echo done
exit
elif [ "$opt" = "Hello" ]; then
echo Hello World
else
clear
echo bad option
fi
done
while [ condition ]
do
command
done
### UNTIL loop
until [ condition ] # executes until condition = true
do
command
done
function e {
echo $1
}
e Hello #will echo Hello when called
#!/bin/bash -x
Adding -x
to the shebang
produces output information
$var #Value of shell variable var.
${var}abc #Example: value of shell variable var with string abc appended.
# #At start of line, indicates a comment.
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.