Introduction
The default shell environment for most GNU/Linux system is Bash ( Bourne Again Shell ).
For any scripting languague in a Linux environment , a script start with a special line called shebang.
Shebang is a line for which #! is prefixed to the environment path. /bin/bash is the interpreter command path for Bash.
Execution of a script can be done in two ways. Either we can run the script as a command-line argument for sh or run a self execution with execution permission. for example:
If a script is run as command-line argument for sh, the shebang in the script is of no use.
The execution permission for the script
can be set as follows:
For self execution script.
In Bash, each command or command sequence is delimited by using a semicolon or a new line.
echo, printf,
-
echo $PATH #/usr/local/bin:/usr/sbin:/usr/bin
-
echo "$PATH" #/user/local/bin:.....
-
echo '$PATH' #$PATH
Note: when using echo with single quotes, the variable inside the quotes will not be interpreted by Bash, but will be display as is .
escaping new line in echo.
-
printf "%-5s -%-10s %-4s\n" No Name Mark
Playing with variable and environment variable
For every process , environment variables in its runtime can be viewed by:
Set the PID with the process ID of the relevent process.
for example. assume that an application called gedit is running . We can obtain the progress ID of gedit with pgrep command as follows.
1250
-
cat /proc/1250/environ | tr '\0' '\n'
-
cat /proc/1250/environ | tr '\0' '\n'
Printing the content of a variable is done using by prefixing $ with the variable name as follows:
-
var="hello" #Assignment of value to variable var.
-
echo $var
There some default system environment variable:
SHELL, USER, HOME, PATH, UID and so on.
Finding length of string
Get the length of a variable value as follow:
for example:
-
var=12345
-
echo ${#var} # 5
Check for super user
-
if [ $UID -ne 0 ]; then
-
echo "Non root user. please run as root."
-
else
-
echo "Root user"
-
fi
Doing math calculations with the shell
The bash shell environment can perform basic arithmatic operations using the commands let, ( ( ) ) and [ ]. The two utilities expr and bc are very helpful performing advanced operations.
While using let, we use variable names without the $ prefix, for example:
-
n1=2;
-
n2=3;
-
let result=n1+n2;
-
let result++;
-
echo $result;
The [ ]
operator can be used similar to the let command as follows:
-
result=$[ n1 + n2 ]; # No space between = and $
expr can be alse used for basic operation.
-
result=`expr 3 + 4`
-
result=$(expr $n1 + 5);
All of above methods do not support floating point numbers, and operate on integer only.
bc the precision calculator is an advanced utility for mathematical operations. It has a wide range of options.
-
echo "4 * 0.56" | bc
-
-
result=`echo "$n1 * 1.5" | bc`
Arrays
-
array_var=(1 2 3 4)
-
array_var[0]="test1"
-
array_var[1]="test2"
-
array_var[2]="test3"
-
varay_var[3]="test4"
-
-
echo ${array_var[0]};
-
#echo $array_var[0]; # the usage is error.
-
-
echo ${array_var[@]}; # print array length or ${array_var[*]}
Function and Arguments
A function can be defined as follows:
-
function fname(){
-
statements;
-
}
$1 is the first argument
$n is the n argument
"$@“ expands as "$1" "$2" "$3" and so on
"$*" expands as "$1c$2c$3", where c is the first character of IFS
”$@" is the most use one.
Store command output
cmd_out=$(COMMAND)
or
cmd_out=`ls | cat -n `
for example:
-
cmd_out=$(ls | cat -n)
-
cmd_out=`ls | cat -n`
Spawning a separate process with subshell
Subshell are separate processes. A subshell can be defined using the
( ) operator as follows:
Read input from Teriminal
read -n number-of-chars variable_name
Read a password in non-echoed mode:
Display a message with read using:
-
read -p "Enter input:" var
-
echo $var
Loop
-
for var in {a..z}
-
do
-
echo $var;
-
done
or
-
for((i=0; i<10; i++)){
-
echo $i | tr '\n' ' '
-
}
-
i=10;
-
while [ $i -gt 0 ]
-
do
-
let i--;
-
echo $i;
-
done
Flow Control and tests
if condition:
-
if condition;
-
then
-
commands;
-
fi
if else condition
-
if condition;
-
then
-
commands;
-
elif condition
-
then
-
commands;
-
else
-
commands;
-
fi
[ $var -eq 0 ]
Note that there is a space between [ or ] and operands.
Multiple test condition can be combined as follows:
-
[ $var -ne 0 -a $var2 -gt 2 ] # using AND -a
-
[ $var -ne 0 -o $var2 -gt 2 ] # OR -o
-gt: great than
-lt less than
-ge great than or equal to
-le less than or equal to
Filesystem related test:
-f if a file.
-x if file is executable.
-d if directory.
-e if file is existed.
-c if file is character device.
-b if file is block device.
-w if file is writable.
-r if file is readable.
-L if file is a symlink.
String comparision
-
[[ $str1 == $str2 ]]; # compare two string
-
[[ -z $str ]]; # Return true if str holds an empty string.
-
[[ -n $str ]]; # Return true if str holds non-empty string.
for example:
-
if [[ -n $str1 ]] && [[ -z $str2 ]];
-
then
-
commands;
-
fi
Slicing filenames based on extension
example 1:
-
filename="sample.jpg";
-
name=${filename%.*}
-
echo File name is $name. # the output is sample
-
-
extension=${filename#*.};
-
echo extension is $extension # the output is jpg
-
-
var=hack.fun.book.txt
-
echo ${var%%.*} # output is hack. because %% is greedy
-
echo ${var##*.} # output is txt. because ## is greedy
Rename file example:
-
count=1;
-
for f in *.txt
-
do
-
newname=${f%%.*}-$count.${f##*.}
-
mv "$f" "$newname" 2>/dev/null
-
if [ $? -eq 0 ];
-
then
-
echo "Renaming $f to $newname"
-
let count++;
-
fi
-
done
阅读(1086) | 评论(0) | 转发(0) |