shell脚本 (实例演示--自己试验下)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本 (实例演示--自己试验下)相关的知识,希望对你有一定的参考价值。

shell脚本 


查看Linux所支持的shell

# cat /etc/shells 

/bin/sh

/bin/bash

/sbin/nologin

/usr/bin/sh

/usr/bin/bash

/usr/sbin/nologin

/bin/tcsh

/bin/csh



输入/输出设备文件

/dev/stdin 标准输入设备 0

/dev/stdout 标准输出设备 1

/dev/stderr 标准错误输出设备 2

重定向

输出重定向

>   >> 仅能重定向标准输出

2>   仅能重定向标准错误输出

&> 同时重定向标准输出及标准错误

输出重定向的特殊文件: /dev/null 

输入重定向


输出语句 

1、echo 


# echo "hello world"    默认换行

hello world


选项:

-n:不换行

# echo -n "hello world"

hello world

 

-e 

# echo -e "hello\tworld\t123"

hello world 123


\t:Tab空白

\n:换行、回车

单引号、双引号的区别

单引号:所有字符失去其特殊含义

双引号:具有特殊含义的字符会被转义 

2、printf

# printf "hello world"

hello world


3、HERE DOCUMENT   用于输出多条信息

# cat << eof

> 1

> 22

> 333

> 4444

> eof

交互式命令的应用


1、passwd 

# echo "redhat" | passwd --stdin tom &> /dev/null 


# echo -e "n\np\n1\n\n+500M\nw\n" | fdisk /dev/sdb &> /dev/null


变量

自定义变量

特殊变量

环境变量

自定义变量


1、声明变量

# 变量名称=变量值


变量值全作为普通字符处理


2、引用变量的值

$变量名称 

${变量名称}


3、使用变量的值进行数学运算

运算符:+ - * /(整除) %(取余)


方法1:$((expression))

# a=10

# b=20

# c=$((a+b))

# echo $c

30


方法2:$[expression]

# c=$[$a+$b]

# echo $c

30


方法3:关键字 let

# let c=a+b

# echo $c

30


方法4:关键字 declare

# declare -i c=a+b

# echo $c

30


4、将命令的执行结果赋予变量

`command`   反引号

$(command)

# a=`ls -ldh /dev`

# a=$(ls -ldh /dev)

# echo $a

drwxr-xr-x. 18 root root 3.2K Jan 24 22:32 /dev


5、删除变量

# unset 变量名称 


特殊变量 

$?    代表上一条命令的执行状态值

0---255:

0:上一条命令执行成功


环境变量 


1、查看环境变量

# env

XDG_SESSION_ID=1

HOSTNAME=server.bj.com

SELINUX_ROLE_REQUESTED=

TERM=xterm

2、声明环境变量

# export 变量名称=变量值

/etc/profile

/etc/bashrc

3、修改环境变量的值

# export 变量名称=new_value

awk

按照指定分隔符分割文本内容

默认以空白字符作为分隔符

# awk ‘{print $1,$4}‘ /1.txt 

this line.

this line.


选项: -F 用于指定分隔符 


# awk -F: ‘{print $1,$7}‘ /etc/passwd

root /bin/bash

bin /sbin/nologin


编写脚本的步骤:

1、新建脚本文件     *.sh

2、编写脚本内容

3、赋予脚本执行权限



编写脚本,实现创建用户userA,并为用户设置初始密码为123456,创建完成后给用户相应提示

#!/bin/bash

#

read -p "please input username: " name


useradd $name

echo "123456" | passwd --stdin $name &> /dev/null

echo "$name create finished,the default password is 123456"



编写脚本,实现配置本地yum源

#!/bin/bash

#


mount /dev/cdrom /mnt &> /dev/null


cat << eof > /etc/yum.repos.d/server.repo

[local]

name=localyum

baseurl=file:///mnt

enabled=1

gpgcheck=0

eof


yum repolist all &> /dev/null

yum makecache


分支语句---------if/case 


if语句:


结构1:

if condition; then 

statement1

statement2

....

fi 


condition格式:

test expression

[ expression ]

[[ expression ]]

command   判断命令的执行状态


条件表达式:

数字判断

字符判断

文件/目录判断



数字判断:

[ number1 -eq number2 ] =

[ number1 -ne number2 ] !=

[ number1 -gt number2 ] >

[ number1 -ge number2 ] >=

[ number1 -lt number2 ] <

[ number1 -le number2 ] <=




编写脚本,由用户输入用户名,如果用户不存在,则创建该用户,并设置初始密码为123456,创建完成后,提示用户创建完成 

#!/bin/bash

#


read -p "please input username for create: " name


grep "^$name:" /etc/passwd &> /dev/null

result=$?


if [ $result -ne 0 ];then

  useradd $name

  echo "123456" | passwd --stdin $name &> /dev/null

  echo "$name create successful,the default password is 123456"

fi




#!/bin/bash

#


read -p "please input username for create: " name



if ! grep "^$name:" /etc/passwd &> /dev/null;then

  useradd $name

  echo "123456" | passwd --stdin $name &> /dev/null

  echo "$name create successful,the default password is 123456"

fi




结构2:单分支if判断 

if condition;then

statement1 

statement2

...

else

statement1

statement2

...

fi



编写脚本,由用户输入用户名,如果用户不存在,则创建该用户,并设置初始密码为123456,并要求用户每一次登录系统时必须更改密码。创建完成后,提示用户创建完成;如果用户存在,显示用户已经存在 

#!/bin/bash

#


read -p "please input username: " name


if id $name &> /dev/null;then

  echo "$name has benn exists..."

else

  useradd $name

  echo "123456" | passwd --stdin $name &> /dev/null

  passwd -e $name &> /dev/null

  echo "$name create successfully,the default password is 123456"

fi



编写脚本,由用户输入名,判断用户的uid与gid是否一致,如果相同,则显示"good user",否则显示"bad user"


#!/bin/bash

#


read -p "please input username: " name


user_id=`grep "^$name:" /etc/passwd | awk -F: ‘{print $3}‘`

group_id=`grep "^$name:" /etc/passwd | awk -F: ‘{print $4}‘`


if [ $user_id -eq $group_id ];then

  echo "Good user..."

else 

  echo "Bad user...."

fi

================================================================================

#!/bin/bash

#


read -p "please input username: " name


user_id=`id -u $name`

group_id=`id -g $name`


if [ $user_id -eq $group_id ];then

  echo "Good user..."

else 

  echo "Bad user...."

fi



编写脚本,由用户输入用户名,判断用户是否存在,如果不存在,就显示用户不存在,如果存在,以下面格式输出用户相关信息:

UserName:

HomeDir:

Shell:



#!/bin/bash

#


read -p "please input username: " name


if id $name &> /dev/null; then

  homedir=`grep "^$name:" /etc/passwd | awk -F: ‘{print $6}‘`

  usersh=`grep "^$name:" /etc/passwd | awk -F: ‘{print $7}‘`

  echo "UserName: $name"

  echo "HomeDir: $homedir"

  echo "UserShell: $usersh"

else

  echo "$name not exist...."

fi



字符判断:

[ str1 == str2 ]  

[ str1 != str2 ]

[ -z str1 ]    空串 




shell脚本中对字符串的处理


1、${#变量名}

作用:返回字符串的长度

# foo="this is a test"

# echo ${#foo}           //返回字符串foo的长度          

14


2、${变量名:offset:length}

作用:截取字符串,length指定截取的长度,也可以不写;字符串的第一个字符的索引值为0

# foo="abcdefg"

# echo ${foo:3:2}     //从下标为3的字符开始截取,共截取2个     

de

# echo ${foo:3}       //从下标为3的字符开始截取到最后的字符     

defg


3、${变量名#pattern}    ${变量名##parttern}

pattern:模式,通配符表达式

作用:清除字符串中符合pattern的字符

# foo="file.txt.zip"

# echo ${foo#*.}          //一个#号代表按照最短匹配清除

txt.zip

# echo ${foo##*.}         //2个#号代表按照最长匹配清除

zip


4、${变量名%pattern}    ${变量名%%parttern}

pattern:模式,通配符表达式

作用:清除字符串中符合pattern的字符,从字符串最后匹配

# echo $foo

file.txt.zip

# echo ${foo%.*}              //1个%代表按照最短匹配

file.txt

# echo ${foo%%.*}           //2个%%代表按照最长匹配

file


5、字符串替换操作

${变量名称/old/new}

# foo="txt.txt"

# echo ${foo/txt/TXT}        替换第1次出现的txt     

TXT.txt

# echo ${foo//txt/TXT}       替换字符串中所有的txt   

TXT.TXT

# echo ${foo/#txt/TXT}       只替换字符串中开头的txt  

TXT.txt

# echo ${foo/%txt/TXT}       只替换字符串中结尾的txt  

txt.TXT


6、实现大小写字母的转换

# foo="ABde"

# echo ${foo,,}      //将字符串foo全部转换成小写        

abde

# echo ${foo,}       //将字符串foo的第1个字符转换成小写

aBde

# echo ${foo^}      //将字符串foo的第1个字符转换成大写 

ABde

# echo ${foo^^}     //将字符串foo全部转换为大写         

ABDE



文件/目录判断 

[ -e filename ]     文件是否存在

[ -f filename ] 是否为普通文本文件

[ -d filename ] 是否为目录 

[ -r filename ] 是否拥有r权限

[ -w filename ] 是否拥有w权限

[ -x filename ] 是否拥有x权限





结构3:多if分支

语法 :

if condition;then 

statement1

statement2

...

elif condition;then 

statement1

statement2

...

elif condition;then 

statement1

statement2

...

else

statement1

statement2

...

fi 


编写脚本,由用户输入文件名,判断文件类型:

.txt     this is a ASC text 

.exe this is windows execute program 

.so this is a libirary file 

#!/bin/bash

#


file_type1="txt"

file_type2="exe"

file_type3="so"


read -p "please input filename: " file

type=${file##*.}


if [ $type == $file_type1 ];then

  echo "This is a ASC text"

elif [ $type == $file_type2 ];then

  echo "This is Windows EXECUTE Program"

elif [ $type == $file_type3 ];then

  echo "This is Linux Libirary File"

else

  echo "Unknown........"

fi



使用逻辑运算符同时判断多个条件: 

And: [ condition1 -a condition2 ]           [ condition1 ] && [ condition2 ]

Or:  [ condition1 -o condition2 ] [ condition1 ] || [ condition2 ]

编写脚本,取出系统时间的小时数,对数据进行判断:

   6--10  this is morning 

   11-13  this is noon 

   其他   this is night 


#!/bin/bash

#


hour=`date +%H`


if [ $hour -ge 6 ] && [ $hour -le 10 ];then

  echo "This is morning..."

elif [ $hour -ge 11 ] && [ $hour -le 13 ];then

  echo "This is noon...."

else

  echo "This is night..."

fi 

结构4:嵌套if

语法:

if condition; then 

  if condition;then 

statement1

statement2

....

  else 

statement1

statement2

...

  fi 

else 

statement1

statement2

...

fi 


编写脚本,判断用户的UID及GID,如果相同则显示"Good User.....",否则显示"Bad User........"

#!/bin/bash

#


read -p "please input username: " name


if id $name &> /dev/null;then

   user_id=`id -u $name`

   group_id=`id -g $name`

   if [ $user_id -eq $group_id ];then

echo "Good user...."

   else

echo "Bad user...."

   fi

else

   echo "$name not exist...."

fi




编写脚本,由用户输入一个文件名,判断该文件是否存在,如果存在,再判断文件中是否存在空白行,则显示该文件中空白行的行数,没有空行,则显示文件内容,并在每行前加入行号;如果不存在,则显示文件不存在

#!/bin/bash

#


read -p "please input filename: " file_name 


if [ -f $file_name ];then

  if grep "^$" $file_name &> /dev/null;then 

     line=`grep "^$" $file_name | wc -l`

     echo "The number of null line is $line"

  else 

     echo "The content of $file_name: " 

     cat -n $file_name

  fi

else

  echo "$file_name not exist..."

fi




编写脚本,配置本地yum源

光盘:挂载,取出挂载点目录 

      未挂载,由用户输入目录名称,并挂载 


生成yum源配置文件:

用户手工输入yum源配置文件名称 

存在, 追加yum源信息 

不存在,创建文件,并生成yum源配置信息


#!/bin/bash

#


# /dev/cdrom


if mount | grep iso9660 &> /dev/null;then

   cdrom_dir=` mount | grep iso9660 | cut -d‘ ‘ -f3`

else

   read -p "please input directory : " cdrom_dir

   if [ -d $cdrom_dir ];then

     mount /dev/cdrom $cdrom_dir &> /dev/null

   else

     mkdir -p $cdrom_dir

     mount /dev/cdrom $cdrom_dir &> /dev/null

   fi

fi


#Edit yum config file


read -p "please input yum config file: " file

if [ -f /etc/yum.repos.d/$file ];then

  echo "[local]" >> /etc/yum.repos.d/$file 

  echo "name=localyum" >> /etc/yum.repos.d/$file 

  echo "baseurl=file://$cdrom_dir" >> /etc/yum.repos.d/$file 

  echo "enabled=1" >> /etc/yum.repos.d/$file 

  echo "gpgcheck=0" >> /etc/yum.repos.d/$file 

else

  echo "[local]" > /etc/yum.repos.d/$file 

  echo "name=localyum" >> /etc/yum.repos.d/$file 

  echo "baseurl=file://$cdrom_dir" >> /etc/yum.repos.d/$file 

以上是关于shell脚本 (实例演示--自己试验下)的主要内容,如果未能解决你的问题,请参考以下文章

shell及编程实例

linux之Shell编程--Shell参数传递实例演示

20.5-20.9 shel:l脚本中的逻辑判断,文件目录属性判断,if特殊用法 ,case判断

iOS开发逆向之应用重签名(下)

shell实现多级菜单脚本编写

Linux之Shell编程(12)--Shell输入/输出重定向实例演示