bash common function memo
background
Occasionally I have to deal with shell scripts at work, but shell scripts are not frequently used tools, and the knowledge system of shell scripts itself is not systematic enough. Every time you use it, some basic usages need to be determined by google. Now, the common functions are specially organized into templates for future reference and use.
Basic Process Control
some instructions
The test command
The test command is used to evaluate an expression and return a boolean value. If the expression evaluation result is true, the "exit status" is 0, and if it is false, the exit status is a non-zero value. Use #? to view the "exit status"
test 1 -eq 1
echo $? # 显示为0 表示1 -eq 1执行结果为true
In addition, you can use [] instead of the test command [logical expression] Note: The logical expression and "[" "]" must have spaces such as
[ 1 -eq 1 ]
The test command usage restriction cannot have regular expression expansion
Integer String File Test
String judgment
Operator Operation Instructions= | "string1" = "string2" | equal to operation |
!= | "string1" != "string2" | not equal to operation |
-z string | -z string | Check if the string length is equal to 0 |
-n string | -n string | Check if string length is not equal to 0 |
Integer comparison operations
Judgment statementint1 –eq int2 | Int1 is equal to int2 |
int1 - not int2 | Int1 is not equal to int2 |
int1 –gt int2 | Int1 is greater than int2 |
int1 –ge int2 | Int1 is greater than or equal to int2 |
int1 –lt int2 | Int1 is less than int2 |
int1 –the int2 | Int1 is less than or equal to int2 |
file attribute judgment
Judgment statement–b filename | Block special file |
–c filename | Character special file |
–d filename | Directory existence |
–f filename | Regular file existence and not a directory |
–g filename | Set-group-ID is set |
–k filename | Sticky bit is set |
–p filename | File is a named pipe |
–r filename | File is readable |
–s filename | File is nonzero size |
–u filename | Set-user-ID bit is set |
–w filename | File is writable |
–x filename | File is executable |
if elif else format
if format
if 逻辑表达式
then
执行语句
fi
There are two ways to write logical expressions with []
if [expression]
if [ 1 != 2]
then
echo "1 != 2"
fi
Give an example of working day or weekend judgment
day_of_week=`date +"%u"`
echo "day_of_work=${day_of_week}"
if [ $day_of_week = 6 ] || [ $day_of_week = 7 ]; then
echo "holiday"
else
echo "workday"
fi
Note that the logical AND operation "||" is between two [ ] rather than expected
[ $day_of_week = 6 || $day_of_week = 7 ]
Use test if test expressions such as
if test 1 != 2
then
echo "1 != 2"
fi
if elif else format
if 条件表达式
then
执行语句
elif 条件表达式
then
执行语句
else
执行语句
fi
code example
#!/bin/sh
current_hour=`date +%H`
echo "currnet time is $current_hour"
if [ ${currnet_hour} -lt 12 ]; then
echo "Good Morning"
elif [ ${currnet_hour} -lt 18 ]; then
echo "Good Afternoon"
else
echo "Good evening"
fi
for loop
Format
for 变量名 in 列表
do
命令
done
print string
for fruit in apple orange banana
do
echo $fruit
done
Display file contents by line
for line in `cat /etc/passwrd`
echo $line
done
Difference between $* and $@
do while
while格式
while command
do
command
done
example
num = 0
while [ $num -lt 10 ]
do
echo -n $num
num = `expr $num + 1`
done
echo "循环之后 num值是 $num "
util command
The util command format is the same as while but opposite to while, the loop will only be executed if the conditional expression is false, and the loop will be exited when the condition is non-zero.
util 条件表达式
do
command
done
shift command
Common operations
datetime operations
Get time date command ####Time format such as display the current date
date "+%Y%m%d"
%n : 下一行
%t : 跳格
%H : 小时(00..23)
%I : 小时(01..12)
%k : 小时(0..23)
%l : 小时(1..12)
%M : 分钟(00..59)
%p : 显示本地 AM 或 PM
%r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
%S : 秒(00..61)
%T : 直接显示时间 (24 小时制)
%X : 相当于 %H:%M:%S
%Z : 显示时区 %a : 星期几 (Sun..Sat)
%A : 星期几 (Sunday..Saturday)
%b : 月份 (Jan..Dec)
%B : 月份 (January..December)
%c : 直接显示日期与时间
%d : 日 (01..31)
%D : 直接显示日期 (mm/dd/yy)
%h : 同 %b
%j : 一年中的第几天 (001..366)
%m : 月份 (01..12)
%U : 一年中的第几周 (00..53) (以 Sunday 为一周的第一天的情形)
%w : 一周中的第几天 (0..6)
%W : Week of the year (00..53) (when Monday is the first day of the week)
%x : Display the date directly (mm/dd/yy)
%y : The last two digits of the year (00.99)
%Y : full year (0000..9999)
Time addition and subtraction
date +%Y%m%d //Display the current day, year, month, day date +%Y%m%d --date="+1 day" //Display the date of the next day
+%Y%m%d -- date="-1 day" //Display the date of the previous day
+%Y%m%d --date="-1 month" //Display the date of the previous month
+%Y%m%d --date ="+1 month" //Display the date of the next month
+%Y%m%d --date="-1 year" //Display the date of the previous year
+%Y%m%d --date ="+1 year" //Display the date of the next year
Or simpler date= date -d -${t}day '+%Y%m%d'
// for t to be a few days ago
function
shell parameters
Variable meaning$0 | filename of the current script |
$n | Arguments passed to scripts or functions. n is a number indicating the number of parameters. For example, the first argument is $1 and the second argument is $2. |
$# | The number of arguments passed to the script or function. |
$* | All arguments passed to the script or function. |
$@ | All arguments passed to the script or function. Slightly different from $* when enclosed in double quotes (" ") |
$? | The exit status of the last command, or the return value of a function. |
$$ | Current shell process ID. For shell scripts, this is the process ID in which the scripts reside. |
function definition
函数格式 function_name () { commands ; commands; } 将函数传入的第一个参数加一并返回
increment () {
sum = \`expr $1 + 1\`;
return $sum;
}
执行函数 increment 2 查看函数执行结果 echo $?
函数参数和返回值 函数总结 shell脚本的函数不能定义形参,这是于通用编程语言的差别 函数内部的变量在函数外也可以返回 函数必须先定义再使用