bash -v test.sh 启用 verbose 调试模式

bash -n test.sh  启用语法检查调试模式

bash -x test.sh  遍历脚本执行过程

一、基础参数

1.shell 中()  {}   []  (())  [[]]

shell 中()  {}   []  (())在 bash shell 中,$( ) 与` ` (反引号) 都可用做命令替换用。
${ }用于变量替换。一般情况下,$var 与${var} 并没有什么不一样,但是用 ${ } 会比较精确的界定变量名称的范围。
$[]和$(())是一样的,都是进行数学运算的。支持+ - * / %(“加、减、乘、除、取模”)。但是注意,bash只能作整数运算,对于浮点数是当作字符串处理的。[[]]  双方括号命令提供了针对字符串比较的高级特性。

2.命令替换

#!/bin/bash
#命令输出负载给变量的两种方式#反引号字符(`)#$()格式
d1=`date`
d2=$(date)echo "The date d1 and time are: $d1"
echo "The date d2 and time are: $d2"

3.  >/dev/null 2>&1

    ls /etcfsdfdsfs   >/dev/null  2>&1  //将标准错误输出导入标准正确输出,然后将标准正确输出重定向到   /dev/null---------------------------------------------------------------------------------------------------------------->/dev/null 2>&1 //  实际上,应该等同于这样: 1>/dev/null 2>/dev/null ,默认情况下就是1,标准输出,所以一般都省略。 而&符号,后面接的是必须的文件描述符。不能写成2>1,这样就成了标准错误重定向到文件名为1的文件中了,而不是重定向标准错误到标准输出中。所以这里就是:标准输出重定向到了/dev/null,而标准错误又重定向到了标准输出,所以就成了标准输出和标准错误都重定向到了/dev/null    2>&1 >/dev/null  //此方式为不正确方式命令从左往右执行,2>&1 为将标准错误输出重定向到标准输出,而这时标准输出是打印在屏幕上的,还没执行到 >/dev/null命令行的重定向什么的, 是在执行命令之前就准备好了的. 解释顺序从左至右依次进行, 2&>1 ,而1是屏幕, 所以标准错误重定向到屏幕, 再而 1>/dev/null ,即标准输出重定向到 /dev/null, 上述2>&1 >/dev/null  并不是什么同一时刻要么产生标准输出要么产生标准错误. 而是两个不同的东西.    

4.数值计算

#在bash中,在将一个数学运算结果赋给某个变量时,可以用美元符和方括号($[ operation ])将数学表达式围起来
#!/bin/bashvar1=100
var2=200var3=$[$var1 * $var2]
echo "$var3"

浮点数计算

#variable=$(echo "options; expression" | bc) 

!/bin/bashvar1=$(echo "scale=2; 6.44/3/56"|bc)
#scale 保留小数点位数
echo $var1

5.退出脚本

#Linux提供了一个专门的变量$?来保存上个已执行命令的退出状态码。对于需要进行检查的命令,必须在其运行完毕后立刻查看或使用$?变量。它的值会变成由shell所执行的最后一条命令的退出状态码
状 态         码 描 述
0             命令成功结束
1             一般性未知错误
2             不适合的shell命令
126         命令不可执行
127         没找到命令
128         无效的退出参数
128+x     与Linux信号x相关的严重错误
130         通过Ctrl+C终止的命令
255         正常范围之外的退出状态码                            

#!/bin/bashvar=$(echo "scale=3; 1000/3.3" |bc )echo $varexit 0

#echo $? 查看状态码#exit 3  可以指定退出的状态码

二、使用结构化命令

1. if-then

#bash shell的if语句会运行if后面的那个命令。如果该命令的退出状态码是0(该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then语句到此结束

if command
then commands
fi 

#!/bin/bash#test multiple commands in the then section
#
#
testuser=zy
#
#then 可以直接跟在if 判断后面 ,也可以另起一行
if grep $testuser /etc/passwd; thenecho "this is my first command"echo "this is my second command"echo "$testuser is exist "fi

if-then-else 语句

#!/bin/bash#test multiple commands in the then section
#
#
testuser=nouser
#if grep $testuser /etc/passwd; thenecho "this is my first command"echo "this is my second command"echo "$testuser is exist "echo
elseecho "the user $testuser does not exist on the system"echo#echo 输出一个空行fi

嵌套 if

#!/bin/bash#test multiple commands in the then section
#
#
testuser=nouser
#if grep $testuser /etc/passwd; thenecho "this is my first command"echo "this is my second command"echo "$testuser is exist "echo
elseecho "the user $testuser does not exist on the system"echoif ls -d /home/$testuser/thenecho "however ,$testuser has a directory"fifi

elif 语句

#!/bin/bash#test multiple commands in the then section
#
#
testuser=$1
#if grep $testuser /etc/passwd; thenecho "this is my first command"echo "this is my second command"echo "$testuser is exist "echo
elif    ls -d /home/$testuser/ &>/dev/null;thenecho "the $testuser dose not exists on the system"echo "however ,$testuser has a directory"else  echo "however ,$testuser dose not exist on the system"echo "$testuser dose not have a directory"    fi
----------------------------------------------------------------------------------------------------[root@localhost shell_script]# ./test8.sh zy
zy:x:22223:1001:IT_2:/home/zy_home:/bin/sh
this is my first command
this is my second command
zy is exist [root@localhost shell_script]# ./test8.sh nouser
the nouser dose not exists on the system
however ,nouser has a directory
[root@localhost shell_script]# ./test8.sh aaa
however ,aaa dose not exist on the system
aaa dose not have a directory
[root@localhost shell_script]# 

if command1
then command set 1
elif command2
then command set 2
elif command3
then command set 3
elif command4
then command set 4
fi 

2.test 命令

test命令提供了在if-then语句中测试不同条件的途径。如果test命令中列出的条件成立,test命令就会退出并返回退出状态码0。这样if-then语句就与其他编程语言中的if-then语句以类似的方式工作了。如果条件不成立,test命令就会退出并返回非零的退出状态码,这使得if-then语句不会再被执行

if test condition
then commands
fi #如果不写test命令的condition部分,它会以非零的退出状态码退出,并执行else语句块

#!/bin/bash
#
#
#
var=$1if test $var
thenecho " condition "else echo "no condition"fi

#bash shell提供了另一种条件测试方法,无需在if-then语句中声明test命令。
if [ condition ]
then commands
fi #方括号定义了测试条件。注意,第一个方括号之后和第二个方括号之前必须加上一个空格,
否则就会报错。
test命令可以判断三类条件:
 数值比较
 字符串比较
 文件比较

比 较             描 述
n1 -eq n2      检查n1是否与n2相等
n1 -ge n2      检查n1是否大于或等于n2
n1 -gt n2       检查n1是否大于n2
n1 -le n2       检查n1是否小于或等于n2
n1 -lt n2        检查n1是否小于n2
n1 -ne n2      检查n1是否不等于n2            

#!/bin/bash
#
#Using numeric test evaluationsval1=$1
val2=$2
#
if [ $val1 -gt 10 ];thenecho "$val1 gt 10"
fiif [ $val1 -eq $val2 ];thenecho "$val1 eq $val2"fi

字符串比较测试
比 较                     描 述
str1 = str2            检查str1是否和str2相同
str1 != str2           检查str1是否和str2不同
str1 < str2            检查str1是否比str2小
str1 > str2            检查str1是否比str2大
-n str1                  检查str1的长度是否非0
-z str1                  检查str1的长度是否为0 

[root@localhost shell_script]# cat test11.sh
#!/bin/bash#
#var1=$1
var2=$2if [ $var1 \> $var2 ];then#字符串比较大于或者小于需要用 \  进行转义,否则会当成文件的重定向echo "$var1 is greater than $var2"
elseecho "$var1 is less than $var2"fi
[root@localhost shell_script]# ./test11.sh aaa cccccc
aaa is less than cccccc
[root@localhost shell_script]# 

test命令的文件比较功能
比 较                     描 述
-d file                 检查file是否存在并是一个目录
-e file                 检查file是否存在
-f file                 检查file是否存在并是一个文件
-r file                 检查file是否存在并可读
-s file                 检查file是否存在并非空
-w file                 检查file是否存在并可写
-x file                 检查file是否存在并可执行
-O file                 检查file是否存在并属当前用户所有
-G file                 检查file是否存在并且默认组与当前用户相同
file1 -nt file2         检查file1是否比file2新
file1 -ot file2         检查file1是否比file2旧

[root@localhost shell_script]# cat test12.sh
#!/bin/bash#Look before you leap var1=$1if [ -d $var1 ];thenecho "the $var1 directory exist"cd $var1ls -a
elseecho "the $var1 directory dose not exists"fi
[root@localhost shell_script]# ./test12.sh /home
the /home directory exist
.  ..  nouser  shell_script  tom  zhengyue  zy_home
[root@localhost shell_script]# 

复合条件测试

if-then语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用:
 [ condition1 ] && [ condition2 ]
 [ condition1 ] || [ condition2 ]

[root@localhost shell_script]# cat test13.sh
#!/bin/bash#
#if [ -d $HOME ] && [ -w $HOME/testing ];thenecho "the file exists and you can write to it"
elif [ -d $HOME ] || [  -w $HONE/testing ];thenecho "the file 2 || 1"
elseecho "I cannot write to the file"fi
[root@localhost shell_script]# ./test13.sh
the file exists and you can write to it
[root@localhost shell_script]# ./test13.sh
the file 2 || 1
[root@localhost shell_script]# 

if-then 的高级特性

bash shell提供了两项可在if-then语句中使用的高级特性:
 用于数学表达式的双括号
 用于高级字符串处理功能的双方括号

双括号命令符号 (())
符 号             描 述
val++             后增
val--             后减
++val             先增
--val             先减
!                 逻辑求反
~                 位求反
**                 幂运算
<<                 左位移
>>                 右位移
&                 位布尔和
|                 位布尔或
&&                 逻辑和
||                 逻辑或

[root@localhost shell_script]# cat test14.sh
#!/bin/bash
i=4
b=$(( i++ ))
c=$(( ++i ))echo $b
echo $c
[root@localhost shell_script]# ./test14.sh
4
6

双方括号命令提供了针对字符串比较的高级特性。双方括号命令的格式如下:
[[ expression ]]

[root@localhost shell_script]# cat test15.sh
#!/bin/bash#
#using pattern matchif [[ $USER == r* ]];thenecho "Hello $USER"
elseecho "sorry , I do not know you"
fi
[root@localhost shell_script]# ./test15.sh
Hello root
[root@localhost shell_script]# 

case 语句

case 字符串 in模式)语句;;模式2 | 模式3)语句;;*)默认执行的 语句;;
esac

#!/bin/bash
#
case $1 in[0-9])echo "$1 IS number";;[A-Z]) echo "$1 is character";;*)echo "$1 is error character";;
esac

[root@localhost shell_script]# cat test16.sh
#!/bin/bashcase $1 in[0-9])echo "$1 is mumber";;[A-Z])echo "$1 is character";;*)echo "$1 is error character";;
esac
[root@localhost shell_script]# ./test16.sh 1
1 is mumber
[root@localhost shell_script]# ./test16.sh A
A is character
[root@localhost shell_script]# ./test16.sh CC
CC is error character
[root@localhost shell_script]# 

只接受start ,stop ,restart ,status#!/bin/bash
#
case $1 in 'start')echo "start server ...";;'stop')echo "stop server ...";;'restart')echo "restart server ...";;'status')echo "running ...";;
*)
echo "`basename $0` {start|stop|restart|status}";;esac##注意多分支判断每个判断结束都要在后面加上;;

for 命令

for var in list
do commands
done 

root@localhost shell_script]# cat test17.sh
#!/bin/bash
#basic for commandfor test in A B C D E F ;doecho "The char : $test"done[root@localhost shell_script]# ./test17.sh
The char : A
The char : B
The char : C
The char : D
The char : E
The char : F
[root@localhost shell_script]# 

[root@localhost shell_script]# cat test18.sh
#!/bin/bash#
#list='A v bg ng jn df ttt'
list=$list' cc'#列表附加内容
for I in $list;doecho "$I"
done
[root@localhost shell_script]# ./test18.sh
A
v
bg
ng
jn
df
ttt
cc
[root@localhost shell_script]

root@localhost shell_script]# cat test19.sh
#!/bin/bash
#
#
for file in /root/* ;doif [ -d "$file" ];thenecho "The $file is a directory"elif [ -f "$file" ];thenecho "the $file is a file"fi
done
[root@localhost shell_script]# ./test19.sh
the /root/1111.cap is a file
The /root/123 is a directory
the /root/222.cap is a file
the /root/anaconda-ks.cfg is a file
the /root/cc is a file
the /root/Joomla_3.9.3-Stable-Full_Package.zip is a file
the /root/ping.out is a file
the /root/qwe.cap is a file
the /root/tr is a file
the /root/:x is a file
[root@localhost shell_script]# 

C 语言的 for命令

for (( variable assignment ; condition ; iteration process ))

[root@localhost shell_script]# cat test20.sh
#!/bin/bash#for (( i=1; i<5; i++ ));doecho "The next number is :$i"done
[root@localhost shell_script]# ./test20.sh
The next number is :1
The next number is :2
The next number is :3
The next number is :4
[root@localhost shell_script]# 

while 命令

while命令的格式是:
while test command
do other commands
done 

#!/bin/bash
#
#
var1=10while    echo $var1[ $var1 -ge 0 ];doecho "This is inside the loop"var1=$[ $var1 - 1 ]
done

until 命令

until命令和while命令工作的方式完全相反。until命令要求你指定一个通常返回非零退
出状态码的测试命令。只有测试命令的退出状态码不为0,bash shell才会执行循环中列出的命令。
一旦测试命令返回了退出状态码0,循环就结束了。

until test commands
do  other commands
done 

root@localhost shell_script]# cat test22.sh
#!/bin/bash#var1=100until echo $var1[ $var1 -eq 0 ];doecho "inside the loop:$var1"var1=$[ $var1 - 25 ]done
[root@localhost shell_script]# ./test22.sh
100
inside the loop:100
75
inside the loop:75
50
inside the loop:50
25
inside the loop:25
0

嵌套循环

#注意,在使用嵌套循环时,你是在迭代中使用迭代,与命令运行的次数是乘积关系。
[root@localhost shell_script]# cat test23.sh
#!/bin/bash#
for (( a=1 ;a<=3;a++ ));doecho "starting loop $a"for ((b=1;b<=3;b++ ));doecho "Inside loop:$b"done
done
[root@localhost shell_script]# ./test23.sh
starting loop 1
Inside loop:1
Inside loop:2
Inside loop:3
starting loop 2
Inside loop:1
Inside loop:2
Inside loop:3
starting loop 3
Inside loop:1
Inside loop:2
Inside loop:3
[root@localhost shell_script]# 

break 语句

break n :指定要跳出的循环层级

[root@localhost shell_script]# cat test24.sh
#!/bin/bash
#
#
#breaking out of a while loopvar1=1while [ $var1 -lt 10 ];doif [ $var1 -eq 5 ];thenbreakfiecho "Iteration: $var1"var1=$[ $var1 + 1 ]
doneecho "The while loop is complated"[root@localhost shell_script]# ./test24.sh
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
The while loop is complated
[root@localhost shell_script]# 

continue 命令

continue命令可以提前中止某次循环中的命令,但并不会完全终止整个循环。

root@localhost shell_script]# cat test25.sh
#!/bin/bash#for (( i=1;i<10;i++ ));doif  [ $i -gt 4 ] && [ $i -lt 7 ];thencontinuefiecho "Iteration : $i"
done
[root@localhost shell_script]# ./test25.sh
Iteration : 1
Iteration : 2
Iteration : 3
Iteration : 4
Iteration : 7
Iteration : 8
Iteration : 9
[root@localhost shell_script]# 

[root@localhost shell_script]# cat test26.sh
#!/bin/bash
# piping a loop to another command
for state in "North Dakota" Connecticut Illinois Alabama Tennessee
do echo "$state is the next place to go"
done | sort #输出结果交给sort 进行排序然后进行输出
[root@localhost shell_script]# ./test26.sh
Alabama is the next place to go
Connecticut is the next place to go
Illinois is the next place to go
North Dakota is the next place to go
Tennessee is the next place to go
[root@localhost shell_script]# 

实例:

[root@localhost shell_script]# cat test27.sh
#!/bin/bashIFS=:
#指定分隔符为:for folder in $PATH ;doecho "path : $folder"
#for 循环遍历path 路径    for file in $folder/*do#for 循环遍历path 路径下的文件,判断是否是可执行文件if [ -x $file ];thenecho "-x :$file"fidone
done

for 命令允许你遍历一系列的值while 命令使用普通命令或者测试命令提供了基于条件命令的循环until 命令也提供了迭代命令的一中方法,但是取非0 的状态进行匹配

#阶乘[root@localhost shell_script]# cat test29.sh
#!/bin/bashfactorial=1for (( number  = 1 ;number <= $1; number++ ));dofactorial=$[ $factorial * $number ]
doneecho "The factorial of $1 is $factorial"
[root@localhost shell_script]# ./test29.sh 8
The factorial of 8 is 40320
[root@localhost shell_script]#

传递9以后的参数

root@localhost shell_script]# cat test30.sh
#!/bin/bashecho $1echo ${10}
echo ${11}

basename 的作用:返回不包含路径的脚本名

[root@localhost shell_script]# cat addem
#!/bin/bash
#testing a multi-function scriptname=$(basename $0)if [ $name = "addem" ];then total=$[ $1 + $2  ]elif [ $name = "multem" ];thentotal=$[ $1 * $2  ]fiecho "$total"[root@localhost shell_script]# ./addem 2 3
5
[root@localhost shell_script]# mv addem multem
[root@localhost shell_script]# ./multem 2 3
6
[root@localhost shell_script]# 

参数统计:$#

root@localhost shell_script]# cat test32.sh
#!/bin/bash# $# 能够获取参数的个数
#getting the number of parametersecho there were $# parameters supplied.exit 0[root@localhost shell_script]# ./test32.sh 3 4  df  h s g h
there were 7 parameters supplied.

关于参数统计的脚本:

 ${$#} 在脚本中会出现异常,需要改成 ${!#}

[root@localhost shell_script]# cat test33.sh
#!/bin/bash
#Testing parameters
#if [ $# -ne 2 ];thenechoecho Usage: test.sh a becho
elsetotal=$[ $1 + $2 ]echoecho the  total is $total  echo
fi——————————————————————————————————————————————————————————————————————————
[root@localhost shell_script]# ./test33.sh 2Usage: test.sh a b[root@localhost shell_script]# ./test33.sh 2 4the total is 6

$* 将给定的多个参数定义为一个整体保存,类似"A B C"

$@ 将给定的多个参数定义为同一字符串的多个单独的单词,类似  "A" "B"  "C"

--------------------------------------

shift 移动变量

在使用shift命令时,默认情况下它会将每个参数变量向左移动一个位置。所以,变量$3的值会移到$2中,变量$2的值会移到$1中,而变量$1的值则会被删除(注意,变量$0的值,

也就是程序名,不会改变)

[root@localhost shell_script]# cat test34.sh
#!/bin/bash
#demonstrating the shift commandcount=1while [ -n "$1" ];do#    "$1" 需要加"" 否则无法进行匹配echoecho "The parameters# $count: $1"echocount=$[ $count + 1 ]shift
done
[root@localhost shell_script]# ./test34.sh A b 1 2 3The parameters# 1: AThe parameters# 2: bThe parameters# 3: 1The parameters# 4: 2The parameters# 5: 3[root@localhost shell_script]# 

处理选项:查找选项

Found the -c option
[root@localhost shell_script]# cat test35.sh
#!/bin/bashecho
while [ -n "$1" ];docase "$1" in-a) echo "Found the -a option" ;;-b) echo "Found the -b option" ;;-c) echo "Found the -c option" ;;*) echo " "$1" is not found option" ;;esac#shift 将每个变量向左移动一位shift
done

[root@localhost shell_script]# ./test35.sh -z -a -v -b -c-z is not found option
Found the -a option-v is not found option
Found the -b option
Found the -c option
[root@localhost shell_script]# 

分离参数和选项:

[root@localhost shell_script]# cat test36.sh
#!/bin/bash
# extracting options and parametersechowhile [ -n "$1" ];docase "$1" in-a) echo "Found the -a option" ;;-b) echo "Found the -b option" ;;-c) echo "Found the -c option" ;;--) shiftbreak ;;*) echo "$1 is not an option" ;;esacshift
done#count=1for param in $@;doecho "Parameter #$count: $param "count=$[ $count + 1 ]
done[root@localhost shell_script]#
[root@localhost shell_script]# ./test36.sh -a -c test ss -- 1 2 3Found the -a option
Found the -c option
test is not an option
ss is not an option
Parameter #1: 1
Parameter #2: 2
Parameter #3: 3
[root@localhost shell_script]# 

处理带值的选项:

[root@localhost shell_script]#  cat test37.sh
#!/bin/bash
# extracting options and parametersechowhile [ -n "$1" ];docase "$1" in-a) echo "Found the -a option" ;;-b) echo "Found the -b option" ;;-c) echo "Found the -c option" echo echo "Found the param :$2 "shift ;;--) shiftbreak ;;*) echo "$1 is not an option" ;;esacshift
done#count=1for param in $@;doecho "Parameter #$count: $param "count=$[ $count + 1 ]
done[root@localhost shell_script]# ./test37.sh -a -c test ss -- 1 2 3Found the -a option
Found the -c optionFound the param :test
ss is not an option
Parameter #1: 1
Parameter #2: 2
Parameter #3: 3
[root@localhost shell_script]# 

getopt 命令:

获得用户输入: read 从键盘获得用户输入

read -t 5 -p "your name: " name

-t  指定超时时间

[root@localhost shell_script]# ./test38.sh
your name: ww
hi ww
your name: ee
ee
[root@localhost shell_script]# cat test38.sh
#!/bin/bash
#testing the read command
#echo -n "your name: "
read name
echo "hi $name"read -p "your name: " name
echo "$name"
[root@localhost shell_script]# ./test38.sh
your name: ff
hi ff
your name: gg
gg
[root@localhost shell_script]# 

[root@localhost shell_script]# cat test39.sh
#!/bin/bash
#getting just one character of input
#read -n1 -p "Do you want to contiune [Y/N]?  " answer
case $answer in
Y|y) echoecho "fine, continue on...";;
N|n) echoecho "OK goodbye"exit;;
esacecho "this is the end of the script"[root@localhost shell_script]# ./test39.sh
Do you want to contiune [Y/N]?  n
OK goodbye
[root@localhost shell_script]# #本例中将-n选项和值1一起使用,告诉read命令在接受单个字符后退出。只要按下单个字符回答后,read命令就会接受输入并将它传给变量,无需按回车键。

# -s选项可以避免在read命令中输入的数据出现在显示器上(实际上,数据会被显示,只是read命令会将文本颜色设成跟背景色一样)。
 

#!/bin/bash
# reading data from a file
#
count=1
cat test | while read line
do echo "Line $count: $line" count=$[ $count + 1]
done
echo "Finished processing the file"#从文件读取内容

root@localhost shell_script]# cat test41.sh
#!/bin/bash
exec 1>testout
#exec 永久重定向
echo "ff"
echo "aa"
echo "bb"[root@localhost shell_script]# cat testout
ff
aa
bb
[root@localhost shell_script]# 

done < ${1} :$1 代表第一个命令行参数

控制脚本

1 SIGHUP      挂起进程
2 SIGINT        终止进程
3 SIGQUIT     停止进程
9 SIGKILL      无条件终止进程
15 SIGTERM    尽可能终止进程
17 SIGSTOP    无条件停止进程,但不是终止进程
18 SIGTSTP    停止或暂停进程,但不终止进程
19 SIGCONT    继续运行停止的进程

trap 捕获信号

root@localhost shell_script]# cat test42.sh
#!/bin/bash
#Testing signal trapping
#trap "echo 'Sorry  I have trap Ctrl-C'" SIGINTecho "This is a test script"count=1
while [ $count -le 10 ];doecho "loop #$count"sleep 1count=$[ $count + 1 ]
doneecho "END"
[root@localhost shell_script]# ./test42.sh
This is a test script
loop #1
loop #2
^CSorry  I have trap Ctrl-C
loop #3
^CSorry  I have trap Ctrl-C
loop #4
^CSorry  I have trap Ctrl-C
loop #5
loop #6
loop #7
loop #8
loop #9
loop #10
END
[root@localhost shell_script]# 

后台运行脚本

./test1.sh &   #后台运行脚本,终端关闭后停止nohup ./test1.sh &  #后台运行脚本,终端关闭后任然执行

nice 命令:

nice -n 10 ./test4.sh > test4.out &

定时任务执行

    1.在未来某个时间点执行一次atbatchat 时间at > COMMANDat > crtl +d :提交指定时间:绝对时间: HH:MM DD:MM:YY MM/DD/YY 相对时间: now+#单位: minutes ,hours ,days ,weeks模糊时间:noon ,midnight ,teatime 命令的执行结果将以邮件的形式发送给安排任务的用户        at -l :显示作业at -d :删除作业at -c ;显示执行的内容2.周期性执行cron :crontab 自身是一个不间断执行的程序anacron: cron 的补充。能够实现cron 没执行的动作cron:系统cron 任务/etc/crontab分钟 小时 天 月 周 用户 任务    用户cron 任务/var/spool/cron/USERNAME时间统配符:**:对应所有有效取值* * * * * :每分钟执行一次     3 * * * * :表示每周每月每天每小时的第三分钟执行一次3 * * * * :每个星期天的每小时的第三分钟执行一次13 12 * * 5 :每周五12 点13 分执行一次13 12 6 7 * :每年7月6号12点13 分执行一次,:离散时间点10,40 * * * * :每小时的第10 分和第40 分执行一次-:连续时间点10 02 * * 1-5 :每周一到周五的2 点 10 分执行一次/:对应取值范围内每多久执行一次*/3 * * * * :每三分钟执行一次* */2 * * * :每隔2 小时执行60 次, 因为每分钟为* 每分钟都会执行01 */2 * * * :每隔 2小时的第一分钟执行一次执行结果将以邮件方式发送给管理员*/3 * * * * cat /etc/fstab > /dev/null :每3 分钟执行一次,并且将正确输出重定向,错误内容邮箱发送cron 环境变量在PATH 查找在脚本中 export 定义环境变量:service crond status :查看crond 服务运行状态crontab -l :查看定时任务crontab -e : 编辑, 注意使用crontab -e 编辑,直接/etc/crontab 不行crontab -r : 移除所有任务crontab -u :指定以哪个用户来运行

转载于:https://www.cnblogs.com/zy09/p/10595554.html

shell 构建脚本基础相关推荐

  1. Gradle2.0用户指南翻译——第六章. 构建脚本基础

    翻译项目请关注Github上的地址: https://github.com/msdx/gradledoc 本文翻译所在分支: https://github.com/msdx/gradledoc/tre ...

  2. Linux shell脚本基础学习

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提. 1. Li ...

  3. SHELL脚本 基础一

    SHELL脚本基础 基本都是干货,都是通过书和视频总结的一小部分,里面应该有一些错误: SHELL变量 1.变量:临时储存数据的,该数据是可以进行变化的数据 2.变量的使用:1.多次重复使用的数据,并 ...

  4. Shell 脚本基础学习 (四)

    现在我们来讨论编写一个脚本的一般步骤.任何优秀的脚本都应该具有帮助和输入参数.并且写一个伪脚本(framework.sh),该脚本包含了大多数脚本都需要的框架结构,是一个非常不错的主意.这时候,在写一 ...

  5. Linux shell脚本基础学习详细介绍(完整版)一

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提. 1. Li ...

  6. Linux shell脚本基础学习详细介绍(完整版)2

    详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...

  7. Linux shell脚本基础学习详细介绍(完整版)

    Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提. 1. Li ...

  8. linux脚本变量运算符,linux——Shell 脚本基础篇(变量类型,变量操作,定义,运算与逻辑关系)...

    Shell 脚本基础 1.变量 什么是变量 #a=1 # echo $a 1 变量:可以变化的量 1.2变量名称注意事项 变量名不能以数字开头 不能与系统中已有的环境变量重名,尽量不要全部使用大写,尽 ...

  9. shell脚本基础 (一)

    shell脚本基础 (一) 如何编写shell脚本: shell脚本存放的路径举例:vi /root/first.sh 1.理清任务: 2.申明shell环境: 3.注释: 4.写粗糙的脚本: 5.完 ...

  10. 详细介绍Linux shell脚本基础学习(一)

    2019独角兽企业重金招聘Python工程师标准>>> Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础 ...

最新文章

  1. tensorflow学习笔记(二十五):ConfigProtoGPU
  2. Spark生态顶级项目汇总
  3. FFmpeg音频编码 ---- pcm转aac(使用新版ffmpeg API,亲测可用)
  4. boost::fusion::fused_function_object用法的测试程序
  5. 什么是技术,技术是什么
  6. Python 序列与映射的解包操作
  7. 三、PHP基础——HTTP协议 文件编程
  8. 图解算法系列笔记(三)
  9. 普通指针到智能指针的转换
  10. 【操作系统/OS笔记18】虚拟文件系统概念
  11. linux 磁盘碎片整理,Linux上没有磁盘碎片清理功能如何整理磁盘碎片
  12. 第十周项目2——贮存班长信息的学生类
  13. 微信支付的架构到底有多牛?
  14. Python爬虫可以爬取什么呢?
  15. 跟着老猫来搞GO——启程
  16. 场景中配置阴影(个人笔记)
  17. ib数学ia选题例子
  18. Oracle BPM 问题
  19. 我与Apache DolphinScheduler的成长之路
  20. 下载安装java(一)

热门文章

  1. 异步保存数据到mysql或mssql 学习笔记
  2. 帆软函数TOIMAGE应用
  3. 传统的6d位姿估计fangfa1_基于视觉的机器人抓取从物体定位、位姿估计到抓取位姿估计 | 公开课预告...
  4. 十进制转bcd码c程序语言,bcd码转换成十进制程序函数
  5. 《python 编程从入门到实践》变量
  6. 用SpringBoot集成Netty开发一个基于WebSocket的聊天室
  7. ActiveMQ学习-Network connectors JAVA代码实现
  8. Python:线程、进程与协程(5)——multiprocessing模块(2)
  9. 更好的使用Java集合(三)
  10. CSS中clear:both用法及事例