shell script
变量和参数:
[root@test test]# mkdir scripts
[root@test test]# cd scripts
[root@test scripts]# pwd
/home/test/scripts
[root@test scripts]# vi variable.sh
#!/bin/bash
a=123
Hello=$a
echo Hello
echo $Hello
echo ${Hello}
echo “$Hello”
[root@test scripts]# sh variable.sh
Hello
123
123
123
说明:
-
在shell中,所有系统变量均为全大写,为了不和系统冲突或者引起混淆,一般不要使用全大写的变量。常见的系统变量有PATH和PWD
-
变量赋值:变量名称=值。“=”左右都不得有空格。左边有空格的话比如a =123,会把a当做命令,=123作为命令的参数来执行
-
变量的使用:$来引用
echo hello #没有对hello变量做引用,仅仅是打印hello字符串
echo $hello #打印出hello变量的值,123
echo KaTeX parse error: Expected 'EOF', got '#' at position 9: {hello} #̲同上,这里的{}相当于界定符,…{Date}http.log
#如果没有{},shell会把整个字串都作为变量处理。
echo “$hello” #仍然打印123,但是引号在shell的变量引用中相当神奇,接下来介绍。
- 命令引用的值赋值给变量
注意:再次引用这个变量时加双引号和不加双引号的区别
-
常用变量$?:用来表示上一条命令的执行结果,0为成功,非0为失败
-
参数传递
运行脚本、运行子shell、调用函数都会涉及参数。常用的参数变量有$0,$1, 2 , 2, 2,@,$*
无论是脚本还是函数,这几个变量的含义是相同的。
l $0 代表脚本或者函数名称
l $1 $2 代表第几个参数
l $@ 代表所有参数,但是每个参数之间默认分隔开
l $* 代表所有参数,并且把所有参数看成连接后的一串字符
[root@test scripts]# vi options.sh
#!/bin/bash
echo “$0 is $0”
echo “$1 is $1”
echo “$2 is $2”
echo “$@ is $@”
echo “$* is $*”
for i in “$@”
do
echo $i
done
for i in “$*”
do
echo $i
done
[root@test scripts]# sh options.sh “aa bb cc” “11 22”
$0 is options.sh
$1 is aa bb cc
$2 is 11 22
$@ is aa bb cc 11 22
$* is aa bb cc 11 22
aa bb cc
11 22
aa bb cc 11 22
如果没有双引号,两个for循环将输出相同的内容
[root@test scripts]# vi options.sh
#!/bin/bash
echo “$0 is $0”
echo “$1 is $1”
echo “$2 is $2”
echo “$@ is $@”
echo “$* is $*”
for i in $@
do
echo $i
done
for i in $*
do
echo $i
done
[root@test scripts]# sh options.sh “aa bb cc” “11 22”
$0 is options.sh
$1 is aa bb cc
$2 is 11 22
$@ is aa bb cc 11 22
$* is aa bb cc 11 22
aa
bb
cc
11
22
aa
bb
cc
11
22
输出
1:标准输出
2:标准错误
重定向是将标准错误和标准输出放到文件(文本文件或者特殊文件/dev/null)中,/dev/null类似于linux的垃圾箱,但是不能找回
只能把标准输出重定向,标准错误还是输出到命令行
2>&1:把标准错误重定向到标准输入
if:
”if” “then” “fi”三个关键字完成了普通if判断的主体。“[ ]”是shell script判断的标志,只要是判断就要放在一对”[ ]”中。(也可以是”[[ ]]”中,这里不做介绍)
方式一:
[root@test scripts]# cat if.sh
#!/bin/bash
ls if.sh > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo “if.sh exists.”
fi
[root@test scripts]# sh if.sh
if.sh exists.
注意划红线的空格,这几个空格是必须有的,不然会出现错误。
$?:上一条命令的执行结果,成功是0,失败为非0
if里面至少5个空格
方式二:
-e:存在
-f:存在且为文件
-d:存在且为目录
[root@test test]# cat if.sh
#!/bin/bash
ls if.sh > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo “if.sh exists”
fi
if [ -e if.sh ];then
echo “if.sh exists”
fi
方式三:写在一行
[root@test scripts]# vi if2.sh
#!/bin/bash
ls if.sh > /dev/numm &2>1 ;if [ $? -eq 0 ];then echo “if.sh exists.”;fi
if…elif:
[root@test scripts]# cat elif.sh
#!/bin/bash
if [ -e $1 ];then
echo “$1 exists.”
else
echo “$1 does not exists.”
fi
[root@test scripts]# sh elif.sh (没有输入参数也判断存在,所以要对参数进行异常判断)
exists.
[root@test scripts]# sh elif.sh is.sh
is.sh does not exists.
[root@test scripts]# sh elif.sh if.sh
if.sh exists.
if…elif…else:
[root@test scripts]# cat ifelse.sh
#!/bin/bash
ls $1 > /dev/null 2>&1
Flag=$?
if [ $Flag -eq 0 ];then
echo “$1 exists.”
elif [ $Flag -eq 2 ];then
echo “$1 is not found”
else
echo “unknow error.”
fi
[root@test scripts]# sh ifelse.sh ifelse.sh
ifelse.sh exists.