shell script的基本使用.

一:认识 shell script 的作用:

(1):运行方式:

   1:直接命令下达:shell.sh文件必须要具备可读与可运行(rx)的权限。

      •绝对路径:使用/home/dmtsai/first.sh来下达命令。

      •相对路径:假设工作目录在/home/dmtsai/,则使用./first.sh来运行。

      •变量“PATH”功能:将first.sh放在PATH指定的目录内,如~/bin/

   2:以bash程序来运行:通过“bash first.sh”或“sh first.sh”来运行。

   3:使用-n及-x来检查与追踪shell文件的语法是否正确。

   4:编写第一个shell script:

    

 运行:

[root@server1 dir]# sh first.sh
Hi Mr dengan
[root@server1 dir]# ./first.sh
Hi Mr dengan
[root@server1 dir]# bash first.sh
Hi Mr dengan
[root@server1 dir]# echo $? //通过exit 1:来表达当程序正确执行完后给系统传回一个1,保存在$?中.
1

export PATH :是将其设为全局变量,
#!/bin/bash //在宣告这个script使用的shell名称。
PATH:可让这个程序在运行时直接执行一些外部命令,而不必写绝对路径。

(2):输入:

session.sh

#!/bin/bash #Introduce: #User inputs his name and age.program show it in screen. #History: #2020/3/15 PATH=/bin:/sbin/:/usr/bin/:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "please input your name:" name #提示使用者输入 read -p "Input your age:" age echo -e "a hello $name" echo -e "Your age is $age" exit 1 ~
操作:
[root@server1 dir]# sh session.sh
please input your name:deng
Input your age:12

 hello deng
Your age is 12

 二:使用判断式:

(1):&&和||的基本用法:
cmd1 && cmd2
1,若cmd1执行完毕之后且正确执行($?=0),则开始执行cmd2。
2,若cmd2执行完毕之后且执行错误($? not equal 0),则cmd2不执行。
cmd1 || cmd2
1,若cmd1执行完毕之后且正确执行,则不执行cmd2.
2,若cmd2执行完毕之后错误执行,则开始执行cmd2

[dengzhaoxu@server1 ~]$ test -e /dmtsai && echo "exit" || echo "Not exit"
Not exit
(2):test命令
test:命令的选项: -e 判断该文件名(目录)是否存在 , -f :判断该文件名(目录)是否存在且为文件名 , -d :判断该文件名(目录)是否存在且为目录名.
-r:判断该文件是否存在且为读文件,  -u:判断该文件是否存在且具有SUID特殊权限. -s:判断该文件是否存在且为非空文件.
-nt:判断file1是否比file2新, eq:判度两个整数是否相等.
 le:小于等于,ge:大于等于,lt小于
例子:
-rw-r--r--. 1 0 0 342 3月  15 22:56 ~
-rwxrwxrwx. 1 0 0 239 3月  15 22:38 first.sh
-rw-r--r--. 1 0 0  21 3月  15 22:28 path
-rw-r--r--. 1 0 0   0 3月  15 22:29 pwd
-rwxrwxrwx. 1 0 0 342 3月  15 23:01 session.sh
-rwxrwxrwx. 1 0 0   0 3月  11 16:28 test
[dengzhaoxu@server1 ~]$ ls -a dir
~  .  ..  first.sh  path  pwd  session.sh  test
[dengzhaoxu@server1 ~]$ cat session
cat: session: 没有那个文件或目录
[dengzhaoxu@server1 ~]$ cat session.sh
cat: session.sh: 没有那个文件或目录
[dengzhaoxu@server1 ~]$ vim session.sh
[dengzhaoxu@server1 ~]$ test path -nt pwd
[dengzhaoxu@server1 ~]$ test path -nt pwd && echo "exit" || echo "no"
no
[dengzhaoxu@server1 ~]$ test 2 -eq 2 && echo "yes" || echo "no" //判断两个整数是否相等.
yes
[dengzhaoxu@server1 ~]$ test 2 -ne 3 && echo "yes" || echo "no" //判断两数值不想等。
yes
[dengzhaoxu@server1 ~]$ str=deng
[dengzhaoxu@server1 ~]$ str1=deng1
[dengzhaoxu@server1 ~]$ test $str1 = $str && echo "yes" || echo "no" //判断字符串是否相等.
no
[dengzhaoxu@server1 ~]$ test -r path -a -x path && echo "yes" || echo "no" //-a判断两种状况是否同时成立,若同时成立则返回true
no
[dengzhaoxu@server1 ~]$ test ! -x path && echo "yes" || echo "no" //当path文件不具备x权限时返回true.
yes
(3):test3.sh:执行该shel script

[root@server1 dir]# cat test3.sh
#!/bin/bash
#Program:
#User input a filename,program  will check the flowing:
#History:
#2018/08/25
PATH=/bin:/sbin/:usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "please input a fileame,I will check the filename's type and permission. "
read -p "Input a filename:" filename
test -z $filename && echo "The filename '$filename' DO NOT exit" && exit 0
#开始判断文件属性和类型
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="perm writable"
#开始输出信息
echo "The filename :$filename is a $filetype"
echo "And the permission are :$perm:"

[root@server1 dir]# sh test3.sh
please input a fileame,I will check the filename's type and permission.
Input a filename:test2
The filename :test2 is a regulare file
And the permission are :perm writable:
(4):使用[]判断符合:
[root@server1 dir]# name="deng"
[root@server1 dir]# [ $name == "deng1" ]&& echo "yes" || echo "no" //
n

三:使用条件判断式:

test3.sh
#!/bin/bash #Program: #This program shows the user's choice #History: #2018/08/25 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input(Y/N):" yn if[ "$yn "== "Y" ] || [ "$yn" == "y" ];then echo "OK,continue" exit 0 fi if[ "$yn" == "N" ]||[ "$yn" == "n" ];then echo "Oh,interrupt!" fi echo "I don't know what your choice is" && exit 0


test5.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input(Y/N):" yn
if[ "$yn"=="Y" ]||[ "$yn"=="y" ];then
echo "OK,continue"
elif[ "$yn"=="N" ]||[ "$yn"=="n" ];then
echo "Oh,interrupt!"
else
echo "i don't konow"
fi


 四:使用date命令和了解脚本在不同的环境下运行的差异:

(1):使用bash(sh)来运行脚本时,该脚本是在一个新的bash环境下运行,那么该脚本中的变量也同样在不同的空间中.实质上他是在子程序bash中。

(2):source 运行脚本则在父程序bash中运行,那么变量也会在父空间中.

代码:

#!/bin/bash
#Program:
#实现:每天都会i创建一个不同的文件,即根据日期的不同来创建文件.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "I will use 'touch' command to create three files"
read -p "please input your filename:" filename
#为了避免用户随意按enter键,利用变量功能分析文件名是否设置.
#filename = ${fileuser:-"filename"}
#利用不同的日期来创建不同的文件名
date1=$(date --date='2 days ago' +%Y%m%d) #获取前两天的日期
date2=$(date --date='1 days ago' +%Y%m%d) #前一天的日期
date3=$(date +%Y%m%d) #今天的日期
#设置文件名
file1=${filename}${date1}
file2=${filename}${date2}
file3=${filename}${date3}
#创建文件
touch "$file1"
touch "$file2"
touch "$file3"
~                                                                               
~  
1:在父程序bash中运行
root@server1 dir]# source test6.sh
I will use 'touch' command to create three files
please input your filename:fuu
[root@server1 dir]# echo $file1
fuu20200328
[root@server1 dir]# echo $file2
fuu20200329


2:在子程序bash中运行   #说明当来到父程序bash中时子程序中运行的脚本中的变量不能输出。
[root@server1 dir]# sh test6.sh
I will use 'touch' command to create three files
please input your filename:fuu1
[root@server1 dir]# echo $file1  //没有输出fuu120200328
fuu20200328



3:date的使用:
昨天:
date -d'-1 days' +%Y%m%d
date -d'1 days ago' +%Y%m%d
前天:
date -d'-2 day' +%Y%m%d
date -d'2 days ago' +%Y%m%d
明天:
date -d'+1 day' +%Y%m%d
date -d'+1 day next' +%Y%m%d
上个月:
date -d'-1 month' +%Y%m%d-%H%M%S
下个月:
date -d'+1 month' +%Y%m%d-%H%M%S
上个周:
date -d'-1 week' +%Y%m%d-%H%M%S

五:使用declare来定义变量的类型,利用$((计算式))来进行数值运算.

[root@server1 dir]# a=4
[root@server1 dir]# b=5
[root@server1 dir]# declare -i total=$a*$b
[root@server1 dir]# echo total
total
[root@server1 dir]# echo $total
20
[root@server1 dir]# echo $((13%3))
1
[root@server1 dir]# 
原文地址:https://www.cnblogs.com/1314bjwg/p/12463968.html