Shell
Shell
1.1
linux shellshell
linuxshell
shell.sh
.bash
Shell +
1.2
第一行,指定哪个程序来执行脚本
#!/bin/bash 或者 #!/bin/sh 注意:# |
#
第一种方式:
Shell
Sh xxx.sh
Bash xxx.sh
shell
Chmod +x xxx.sh
./xxx.sh
变量
2.1
字母或者下划线开头,不能以数字开头,后面可以接字母,数字,下划线。
2.2
Shell
shellshell
2.3
如果要给变量赋空值,可以在等号后面接一个换行符
在控制台打印变量的值
echo $variable ${variable}
清除变量
unset variable
显示所有变量,包括本地变量和环境变量
Set
如何把命令的输出,赋值给一个变量
` `
A=``a
[hadoop@huawei shellDemo]$ a=`ls` [hadoop@huawei shellDemo]$ echo $a demo01.sh helloWorld01.sh helloWorld.sh ls.sh out specailVar01.sh specailVar02.sh specailVar.sh test [hadoop@huawei shellDemo]$ ls |
2.4
环境变量简称全局变量,按照惯例需要大写
export VAR
shell
shellshell
2.5
shell
shellxxx.sh + +1++2
shell$1 $2
shell
shell
在脚本中需要使用这些参数的时候需要通过位置参量来引用,例如:
#!/bin.sh A=$1 echo $A 结果是:hahhaha |
2.5.1
$0 | 当前脚本的文件名 |
$1-$9 | 第19 |
${10} | 第10${11} , ${12}.... |
$# | 位置参数的个数,注意:你传入几个,就打印几个 |
$* | 以单字符串显示所有位置参数,其实就是把位置参数打印出来 |
$@ | 没有加双引号的时候与$* |
$$ | 脚本运行的当前进程号 |
$! | 最后一个后台运行的进程的进程号 |
$? | 显示前面最后一个命令的退出状态 0 |
案例:
#!/bin/sh echo "the count of parameters:$#" echo "first param=$1" echo "second param=$2" echo "all paramers=$*" 给 shell [hadoop@huawei test]$ sh shell_04.sh this is perter the count of parameters:3 first param=this second param=is all paramers=this is perter 如果位置参数中含有空格,则需要使用双引号 the count of parameters:2 first param=this is second param=perter all paramers=this is perter |
2.5.2
echo $?
$?,sh
exit N
0
0
2.6 shell
语法 { } 1.shell 2.$? return returnn(0-255) |
2.7
格式 :expr m + n $((m+n)) expr 例如计算(2 3 4 1 . 2. expr `expr 2 + 3 ` \* 4 echo `expr \`expr 2 + 3\` \* 4` 或 $(((2+3)*4)) |
3.
Arr=(math english chinese)
Arr =(math english chinese)
${arr[0]}
${#arr[*]}
${arr[*]}
arr[0]=chinese
3.date
Date
Linuxdate
[hadoop@huawei test]$ date 2017100813:12:23 CST |
也可以制定日期格式
[hadoop@huawei test]$ date +%Y/%M/%D 2017/16/10/08/17 注意:m month MINUTE |
之前的时间
date --date='1 days ago' +%y/%m/%d |
4.cal
cal
[hadoop@huawei test]$ cal 日 一 二 三 四 五 六 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
cal year
[hadoop@huawei test]$ cal 2010 31 31 |
cal month year
[hadoop@huawei test]$ cal 12 2010 日 一 二 三 四 五 六 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
5.判断
6.1
test -e filename
-e | 根据文件名判断是否存在 exist |
-f | isFile |
-d | isDirectory |
-r | 判断该文件是否有可读权限 |
-w | 判断该文件是否有可写权限 |
-x | 判断该文件是否有可执行权限 |
6.2
test n1 -eq n2
-eq | 两数值是否相等(equal |
-ne | 两数值不等(not equal |
-gt | n1 n2 greater than |
-lt | n1 less than |
-ge | n1 n2 |
-le | n1 n2 |
判断字符串的数据
Test -z string | 判断字符串是否为0stringtrue |
Test -n string | 判断字符串是否为0stringfalse |
Test str1=str2 | 判断str1 str2true |
Test str1!=str2 | 判断str1str2false |
, [] test$HOME
[hadoop@huawei ~]$ [ -z "$HOME" ];echo $? 1 0 |
注意中括号必须有空格,前面有空格,后面也得有空格
6.If
7.1
如果只有一个判断式要进行,那么我们可以简单的这么看:
If [ ];then fi if |
&& ||
琛ㄧずand
浠h〃or
If
例子:
#!/bin/bash read -p "please input your name:" NAME #printf '%s\n' $NAME if [ $NAME = root ] fi |
sh ./hive_load_day.sh /home/hadoop/data/twnews_context_by_date/
#!/bin/bash function show() { $1 var="$i" varin1="${var#*=}" varin2="'/home/hadoop/data/twnews_context_by_date/sendtime="$varin1"/000000_0'"
vartsp="'${var#*=}'"
show "$i" } show $1 exit 0 |
7.2
If[ ];then else fi |
7.3
if...elif...else
If[ ];then
当条件判断式一成立时,可以进行指令工作内容
elif[ ];then
当条件判断式二成立时,可以进行的指令工作内容
else
当之前的条件判断式不成立,可以进行的指令工作内容;
fi
7.for
For((;;)) do done |
for
i=1
i <= 100
i=i+1
案例: For in s=0 for((i=1;i<=100;i=i+1)) do 1 3 6 10 15 21 28 36 |
第二种:
for N in 1 2 3 do echo $N done 或 for N in 1 2 3; do echo $N; done 或 for N in {1..3}; do echo $N; done |
8.while
while do done,until do done()
一般来说,不定循环最常见的就是底下两种状态:
do
done
While ...condition
until [ condition ]
do
done
whilecondition
第一种 while expression do command … done 第二种 i=1 while ((i<=3)) do done |
9.case
格式 case $1 in start) echo "starting" ;; stop) echo "stoping" ;; *) echo "Usage: {start|stop} esac |
问题:如何把一个命令的输出赋值给某个变量??
A=`ls -l`
echo $A