Linux-shell

  1. Shell脚本通常都是以.sh 为后缀名。.sh为后缀的文件那么它一定会是一个shell脚本了。test.sh中第一行一定是 “#! /bin/bash” 它代表的意思是,该文件使用的是bash语法。如果不设置该行,那么你的shell脚本就不能被执行。

   ’#’表示注释

    在该中@表示调用执行文件。比如说@@是在文件内调用执行文件

      1、直接./加上文件名.sh(该文件必须有x[执行]权限)。加权限:chmod u+x  执行文件名.sh

      2、sh 执行文件名.sh (该执行文件可以没有X权限)

默认我们用vim编辑的文档是不带有执行权限的,所以需要加一个执行权限,那样就可以直接使用’./filename’ 执行这个脚本了。

另外使用sh命令去执行一个shell脚本的时候是可以加-x选项来查看这个脚本执行过程的,这样有利于我们调试这个脚本哪里出了问题。

  2. 修改linux的时间可以使用date指令
  date命令的功能是显示和设置系统日期和时间。
  输入date 查看目前系统时间。
  修改时间需要 date -功能字符 修改内容
  命令中各选项的含义分别为:
  -d datestr, --date datestr 显示由datestr描述的日期
  -s datestr, --set datestr 设置datestr 描述的日期
  -u, --universal 显示或设置通用时间
  时间域
%H 小时(00..23)
%I 小时(01..12)
%k 小时(0..23)
%l 小时(1..12)
%M 分(00..59)
%p 显示出AM或PM
%r 时间(hh:mm:ss AM或PM),12小时
%s 从1970年1月1日00:00:00到目前经历的秒数
%S 秒(00..59)
%T 时间(24小时制)(hh:mm:ss)
%X 显示时间的格式(%H:%M:%S)
%Z 时区 日期域
%a 星期几的简称( Sun..Sat)
%A 星期几的全称( Sunday..Saturday)
%b 月的简称(Jan..Dec)
%B 月的全称(January..December)
%c 日期和时间( Mon Nov 8 14:12:46 CST 1999)
%d 一个月的第几天(01..31)
%D 日期(mm/dd/yy)
%h 和%b选项相同
%j 一年的第几天(001..366)
%m 月(01..12)
%w 一个星期的第几天(0代表星期天)
%W 一年的第几个星期(00..53,星期一为第一天)
%x 显示日期的格式(mm/dd/yy)
%y 年的最后两个数字( 1999则是99)
%Y 年(例如:1970,1996等)
需要特别说明的是,只有超级用户才能用date命令设置时间,一般用户只能用date命令显示时间。

[weblogic@ods-o1 text]$ date 
Thu Oct 12 15:42:25 CST 2017
[weblogic@ods-o1 text]$ date "+%y%m%d %H:%M:%S"
171012 15:42:28
[weblogic@ods-o1 text]$ date "+%y%m%d"
171012
[weblogic@ods-o1 text]$ date "+%Y%m%d"
20171012
[weblogic@ods-o1 text]$ date -d "-1 month" "+%Y%m%d"
20170912
[weblogic@ods-o1 text]$ date -d "-1 year" "+%Y%m%d"
20161012
[weblogic@ods-o1 text]$ date -d "-2 day" "+%Y%m%d"
20171010
[weblogic@ods-o1 text]$

变量

这里讲一下Linux中三种引号:

1、反引号``(tab键上方):命令替换

在输出一句话的时候,如果想中间加入命令输出结果,在反引号里面输入命令就可以做到,和$COMMAND是一样的。

[weblogic@ods-o1 text]$ vim test2.sh

#! /bin/bash
d=`date +%H:%M:%S`
echo "the script begin at $d"
echo "now we will sleep 3 seconds."
sleep 3
d1=`date +%H:%M:%S`
echo "the script end at $d1"

"test2.sh" 7L, 155C written                                                                            
[weblogic@ods-o1 text]$ sh test2.sh
the script begin at 15:53:17
now we will sleep 3 seconds.
the script end at 15:53:20

2、"":弱引用,可以实现变量和命令的替换。

在双引号中可以使用变量,用$COMMAND。

3、'':强引用,不完成变量替换

数学计算要用’[ ]’括起来并且外头要带一个’$’

[weblogic@ods-o1 text]$ vim test3.sh

#! /bin/bash
a=1
b=2
sum=$[$a+$b]
echo "sum is $sum"

"test3.sh" [New] 5L, 53C written                                                                       
[weblogic@ods-o1 text]$ sh test3.sh
sum is 3
[weblogic@ods-o1 text]$ vim test4.sh

#! /bin/bash
echo "please input a number"
read x
echo "please input another number"
read y
sum=$[$x+$y]
echo "the sum of number is:$sum"

"test4.sh" [New] 7L, 137C written                                                                      
[weblogic@ods-o1 text]$ sh test4.sh
please input a number
34
please input another number
3
the sum of number is:37
[weblogic@ods-o1 text]$ 
[weblogic@ods-o1 text]$ vim test5.sh

#! /bin/bash
read -p "please input a number:" x
read -p "please input another number:" y
sum=$[$x+$y]
echo "the sum of two numbers is : $sum."

"test5.sh" [New] 6L, 144C written                                                                      
[weblogic@ods-o1 text]$ sh test5.sh
please input a number:5
please input another number:6
the sum of two numbers is : 11.

test4.sh的简化版。其中 read -p 选项类似echo的作用。

命令多加个-x  :sh  -x  文件名.sh  会把执行过程一步步解析出来

[weblogic@ods-o1 text]$ sh -x test4.sh
+ echo 'please input a number'
please input a number
+ read x
3
+ echo 'please input another number'
please input another number
+ read y
4
+ sum=7
+ echo 'the sum of number is:7'
the sum of number is:7
[weblogic@ods-o1 text]$ sh -x test2.sh
++ date +%H:%M:%S
+ d=16:09:52
+ echo 'the script begin at 16:09:52'
the script begin at 16:09:52
+ echo 'now we will sleep 3 seconds.'
now we will sleep 3 seconds.
+ sleep 3
++ date +%H:%M:%S
+ d1=16:09:55
+ echo 'the script end at 16:09:55'
the script end at 16:09:55
[weblogic@ods-o1 text]$ 

实际上,shell脚本在执行的时候后边是可以跟变量的,而且还可以跟多个。

shell脚本的预设变量

哪里来的$1和$2,这其实就是shell脚本的预设变量,其中$1的值就是在执行的时候输入的1,而$2的值就是执行的时候输入的$2,当然一个shell脚本的预设变量是没有限制的,这回你明白了吧。另外还有一个$0,不过它代表的是脚本本身的名字。不妨把脚本修改一下。

[weblogic@ods-o1 text]$ vim test6.sh

#! /bin/bash
sum=$[$1+$2]
echo "$0 $sum"

"test6.sh" 3L, 41C written                                                                             
[weblogic@ods-o1 text]$ sh test6.sh 4 3
test6.sh 7
[weblogic@ods-o1 text]$ 

【shell脚本中的逻辑判断

1)不带else

if 判断语句; then

command

fi

[weblogic@ods-o1 text]$ vim test7.sh

#! /bin/bash
read -p "please input your score:" a
if((a<60));then
        echo "you didn't pass the exam."
fi

2)带有else

if 判断语句 ; then

command

else

command

fi

[weblogic@ods-o1 text]$ vim test7.sh

#! /bin/bash
read -p "please input your score:" a
if((a<60));then
        echo "you didn't pass the exam."
else
   echo "success"
fi

3)带有elif

if 判断语句一 ; then

command

elif 判断语句二; then

command

else

command

fi

这里的 && 表示“并且”的意思,当然你也可以使用 || 表示“或者”,执行结果:

[weblogic@ods-o1 text]$ vim test7.sh

#! /bin/bash
read -p "please input your score:" a
if((a<60));then
        echo "you didn't pass the exam."
elif((a<70))&&((a=66));then
   echo "good"
else
        echo "success"
fi

在判断数值大小除了可以用”(( ))”的形式外,还可以使用”[ ]”。但是就不能使用>, < , = 这样的符号了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。


[weblogic@ods-o1 text]$ a=10;if[$a -lt 5];then echo ok;fi
-bash: syntax error near unexpected token `then'
[weblogic@ods-o1 text]$ a=10;if[$a -lt 5]; then echo ok;fi
-bash: syntax error near unexpected token `then'
[weblogic@ods-o1 text]$ a=10;if[$a -lt 5 ]; then echo ok; fi
-bash: syntax error near unexpected token `then'
[weblogic@ods-o1 text]$ a=10; if [ $a -ge 10 ]; then echo ok; fi
ok
[weblogic@ods-o1 text]$ a=10; if [ $a -lt 10 ]; then echo ok; fi
[weblogic@ods-o1 text]$ a=10; if [ $a -gt 10 ]; then echo ok; fi
[weblogic@ods-o1 text]$ a=10; if [ $a -le 10 ]; then echo ok; fi
ok
[weblogic@ods-o1 text]$ a=10; if [ $a -eq 10 ]; then echo ok; fi
ok
[weblogic@ods-o1 text]$ a=10; if [ $a -ne 10 ]; then echo ok; fi
[weblogic@ods-o1 text]$ a=8; if [ $a -lt 10 ] && [ $a -gt 5 ]; then echo ok ; fi
ok
[weblogic@ods-o1 text]$ a=8; if [ $a -lt 10 ] || [ $a -gt 5 ]; then echo ok ; fi
ok
[weblogic@ods-o1 text]$ a=10; if [ $a -lt 10 ] || [ $a -gt 5 ]; then echo ok ; fi
ok
[weblogic@ods-o1 text]$ a=10; if [ $a -ne 10 ] || [ $a -gt 5 ]; then echo ok else echo off; fi
ok else echo off
[weblogic@ods-o1 text]$ a=10; if [ $a -ne 10 ] || [ $a -gt 5 ]; then echo ok; else echo off; fi
ok
View Code



shell 脚本中if还经常判断关于档案属性,比如判断是普通文件还是目录,判断文件是否有读写执行权限等。常用的也就几个选项:

-e :判断文件或目录是否存在

-d :判断是不是目录,并是否存在

-f :判断是否是普通文件,并存在

-r :判断文档是否有读权限

-w :判断是否有写权限

-x :判断是否可执行

使用if判断时,具体格式为: if [ -e filename ] ; then

文件判断

在shell 脚本中,除了用if来判断逻辑外,还有一种常用的方式,那就是case了。具体格式为:

case 变量 in

value1)

command

;;

value2)

command

;;

value3)

command

;;

*)

command

;;

esac

[weblogic@ods-o1 text]$ vi tes8.sh

#! /bin/bash
read -p "please input a number:" n
a=$[$n%2]
case $a in
1)
        echo "odd"
        ;;
0)
        echo "even"
        ;;
esac

"tes8.sh" [New] 11L, 113C written                                                                      
[weblogic@ods-o1 text]$ sh tes8.sh
please input a number:4
even
[weblogic@ods-o1 text]$ sh tes8.sh
please input a number:5   
odd
case

【shell脚本中的循环

1、

for 变量名 in 循环的条件; do

command

done

2、

while 条件; do

command

done

[weblogic@ods-o1 text]$ vi for.sh

#! /bin/bash
for i in `seq 1 5`; do
        echo $i
done

a=10;
while [ $a -ge 1 ]; do
        echo "$a"
        a=$[$a-1]
done

~
"for.sh" [New] 11L, 108C written                                                                       
[weblogic@ods-o1 text]$ sh for.sh
1
2
3
4
5
10
9
8
7
6
5
4
3
2
1
[weblogic@ods-o1 text]$ 

【shell脚本中的函数

function 函数名() {

command

}

[weblogic@ods-o1 text]$ vi fun.sh

#! /bin/bash
function sum(){
        sum=$[$1+$2]
        echo $sum
}
sum $1 $2

"fun.sh" [New] 6L, 66C written                                                                         
[weblogic@ods-o1 text]$ sh fun.sh 1 4
5
[weblogic@ods-o1 text]$ 
函数

在shell脚本中,函数一定要写在最前面,不能出现在中间或者最后,因为函数是要被调用的,如果还没有出现就被调用,肯定是会出错的。

原文地址:https://www.cnblogs.com/huadiyatou/p/7654809.html