for linux shell 菜鸟_《Linux菜鸟入门2》脚本基础及脚本

脚本

●BASH=GNU Bourne-Again Shell,BASH是GNU组织开发和推广的一个项目。

Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操作,并可以完成图形工具无法实现的功能。

[1.如何创建新shell脚本]

1.创建包含bash命令的文本文件(一般文件名后加.sh),文件第一行:

#!/bin/bash

2.使文件可执行(chmod +x scripts)

3.将文件放置在用户的$PATH的目录中

~/bin    用于用户的私有程序

/usr/local/bin   本地开发、系统上的其他人使用的脚本

/usr/local/sbin  本地开发、由root使用的脚本

运行脚本:sh+文件名或者直接编写文件所在绝对路径

例如:

[root@web1 mnt]# vim 1.sh      编写脚本(以下为内容)

#!/bin/bash

echo hello world

[root@web1 mnt]# chmod +x 1.sh   给可执行权限

[root@web1 mnt]# /mnt/1.sh       运行脚本

hello world

[root@web1 mnt]# sh 1.sh         运行脚本

hello world

[root@web1 mnt]# vim 1.sh

#!/usr/bin/env/tcsh -x

cat /mnt/1.sh

[root@web1 mnt]# sh -x 1.sh   (对于/usr/bin/env这种脚本,执行时用sh)

+ cat /mnt/1.sh

#!/usr/bin/env tcsh -x

cat /mnt/1.sh

[root@web1 mnt]# echo $2   (特殊字符如$ ! `` # *等需要在前加转义字符\才能显示)

[root@web1 mnt]# echo \$2

$2

[root@web1 mnt]# echo '\$2'   (''强化显示)

\$2

[root@web1 mnt]# echo "\$2"   (""弱化显示)

$2

[root@web1 mnt]# echo "''"

''

[root@web1 mnt]# echo '""'

""

[root@web1 mnt]# echo \'\'

''

[root@web1 mnt]# echo ****** ******      (显示当前目录的所有内容)

1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private 1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private

[root@web1 mnt]# echo "****** ******"    (要想显示**需要加"")

****** ******

[root@web1 mnt]# echo "****** `date` ******"

****** Tue Dec 13 08:39:41 EST 2016 ******

[root@web1 mnt]# echo '****** `date` ******'

****** `date` ******

[root@web1 mnt]# a=1        给a一个值

[root@web1 mnt]# echo $a    输出a的值

1

[root@web1 mnt]# echo $ab

[root@web1 mnt]# echo ${a}b

1b

[root@web1 mnt]# a=`date`

[root@web1 mnt]# echo $a

Tue Dec 13 08:47:17 EST 2016

[命令替换]

[root@web1 mnt]# s=`date`

[root@web1 mnt]# echo $s

Wed Dec 14 08:18:20 EST 2016

[root@web1 mnt]# vim 4.sh

[root@web1 mnt]# echo $s

Wed Dec 14 08:18:20 EST 2016

[root@web1 mnt]# sh 4.sh

[root@web1 mnt]# s=1

[root@web1 mnt]# sh 4.sh

[root@web1 mnt]# export s=1

[root@web1 mnt]# sh 4.sh

1

[root@web1 mnt]# cd

[root@web1 ~]# chmod +x /mnt/4.sh   加执行权限

[root@web1 ~]# /mnt/4.sh

[root@web1 ~]# vim .bash_profile

PATH=$PATH:$HOME/bin

export PATH

s=1           添加设置s的值为1

[root@web1 ~]# source .bash_profile   要执行脚本须执行此命令

[root@web1 ~]# /mnt/4.sh

1

[root@web1 ~]# su - student

Last login: Wed Dec 14 07:57:25 EST 2016 on pts/0

[student@web1 ~]$ /mnt/4.sh

[student@web1 ~]$ logout

[root@web1 ~]# vim /etc/profile     配置文件

[root@web1 ~]# /mnt/4.sh

1

[root@web1 ~]# source /etc/profile   刷新

[root@web1 ~]# /mnt/4.sh

10

[root@web1 ~]# logout

Connection to 172.25.254.149 closed.

[kiosk@foundation49 Desktop]$ ssh root@172.25.254.149

root@172.25.254.149's password:

Last login: Wed Dec 14 08:05:53 2016 from 172.25.254.49

[root@web1 ~]# /mnt/4.sh     重新登陆时脚本将变为初次设置的值(vim .bash_profile中设置的值)

1

[root@web1 ~]# su - student

Last login: Wed Dec 14 08:22:10 EST 2016 on pts/0

[student@web1 ~]$ /mnt/4.sh   其他用户

10

[student@web1 ~]$

[root@web1 ~]# vim .bash_profile

export PATH

export s=1

export PATH=$PATH:/mnt

[root@web1 ~]# source .bash_profile

[root@web1 ~]# 4.sh

1

[root@web1 ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/mnt

[shell计算命令]

++  自增

--  自减

-   减法     +  加法

**  幂运算   *  乘法

/   除法     %  余数

+=  加等     -= 减等

[root@web1 ~]# echo $[1+2]

3

[root@web1 ~]# echo $[2*2]

4

[root@web1 ~]# echo $[2**3]

8

[root@web1 ~]# echo $[2/3]

0

[root@web1 ~]# echo $[2%3]

2

[root@web1 ~]# echo `expr 1 + 2 `   用expr表示数学运算

3

[root@web1 ~]# let a=1+2      用let指示数学运算

[root@web1 ~]# echo $a

3

[root@web1 ~]# ((a=3+4))    用(())表示数学运算。bash内建功能,效率高

[root@web1 ~]# echo $a

7

[root@web1 ~]# a=2+3        此种写法不是运算会按照字符输出

[root@web1 ~]# echo $a

2+3

查看文件属性:

test -{b|c|e|f|d|r|w|x|s|L} file/directory

[ -b /dev/sda ];echo $?判断文件是否为一个block device

1

[ -c /dev/sda ];echo $?判断文件是否存在且是一个character device

0

[ -e /dev/sda ];echo $?判断文件是否存在,常用

0

[ -f /dev/sda ];echo $?判断文件是否存在且为文件(file),常用

0

[ -d /dev/sda ];echo $?判断文件是否存在且为目录(directory),常用

1

[ -L /dev/sda ];echo $?判断文件是否存在且为一个链接文件

1

test -{zn} string

test -z string判断字符串是否为0?若string为空字符串,则为true

test -n string判断判断字符串是否非为0?若string为空字符串,则为false

[root@web1 ~]# for (( i=1;i<=10;i++ )) ; do echo $i; done

[root@web1 ~]# for (( i=1;i<10;i++ )) ; do ((j+=i)); echo $j; done

1

3

6

10

15

21

28

36

45

脚本常用语句(for;do;done语句,while;do;done语句等)

脚本---> 10秒倒计时脚本:

[root@web1 mnt]# vim time.sh

[root@web1 mnt]# sh time.sh       执行脚本

[root@web1 mnt]# cat time.sh

#!/bin/bash

for ((SEC=10;SEC>0;SEC--))

do

echo -ne "After ${SEC}s is end"

echo -ne "\r  \r"                  换行

sleep 1                            停顿1秒

Done

脚本---> 一分十秒倒计时脚本;

[root@web1 mnt]# vim time.sh

[root@web1 mnt]# sh time.sh

After 0:56s is end^C

[root@web1 mnt]# cat time.sh

#!/bin/bash

MIN=1

for ((SEC=10;SEC>=0;SEC--))

do

echo -ne "After ${MIN}:${SEC}s is end"

sleep 1

echo -ne "\r  \r"

while [ "$SEC" -le "0" -a "$MIN" -gt "0" ]

do

echo -ne "After ${MIN}:${SEC}s is end"

echo -ne "\r  \r"

((MIN--))

SEC=60

done

Done

脚本---> 连接172.25.254.x 能ping通显示is up,不能ping通显示:

[root@web1 mnt]# vim ping.sh   is down的脚本

[root@web1 mnt]# sh ping.sh

172.25.254.1 is up

172.25.254.2 is up

172.25.254.3 is up

172.25.254.4 is up

172.25.254.5 is up

172.25.254.6 is up

172.25.254.7 is up

172.25.254.8 is down

172.25.254.9 is up

172.25.254.10 is up

[root@web1 mnt]# cat ping.sh

#!/biin/bash

for NUM in {1..10}

do

ping -c1 -w1 172.25.254.$NUM &> /dev/null

while

[ "$?" -eq "0" ]

do

echo 172.25.254.$NUM is up

break

done

while

[ "$?" -ne "0" ]

do

echo 172.25.254.$NUM is down

done

done

也可以是第二种:

[root@web1 mnt]# vim ping.sh

[root@web1 mnt]# cat ping.sh

#!/biin/bash

for NUM in {1..10}

do

ping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is up || echo 172.25.254.$NUM is down

done

[root@web1 mnt]# bash -x ping.sh   查看脚本信息

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.1

+ echo 172.25.254.1 is up

172.25.254.1 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.2

+ echo 172.25.254.2 is up

172.25.254.2 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.3

+ echo 172.25.254.3 is up

172.25.254.3 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.4

+ echo 172.25.254.4 is up

172.25.254.4 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.5

+ echo 172.25.254.5 is up

172.25.254.5 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.6

+ echo 172.25.254.6 is up

172.25.254.6 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.7

+ echo 172.25.254.7 is up

172.25.254.7 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.8

+ echo 172.25.254.8 is down

172.25.254.8 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.9

+ echo 172.25.254.9 is up

172.25.254.9 is down

+ for NUM in '{1..10}'

+ ping -c1 -w1 172.25.254.10

+ echo 172.25.254.10 is up

172.25.254.10 is down

脚本---> 数据库备份:

vim MsqDump.sh

#!/bin/bash

for x in $(mysql -uroot -predhat -e "show databases;"   -NE | grep -E "^\*|schema$"-v)

do

mysqldump -uroot -predhat $x >/mnt/$x.dump

done

[]数字运算比较符 -z 为空 -n 不为空

-eq 等于 -lt小于  -le小于等于  -gt 大于  -ge大于等于

文件状态运算符:

-d 设备 -c字符 -e是否可执行 -L软链接 -d目录 -f普通文件

1.Vim time.sh

#!/bin/bash

HOUR=1

MIN=1

for ((SEC=10;SEC>=0;SEC--))

do

echo -ne "After ${MIN}:${SEC}s is end"

sleep 1

echo -ne "\r  \r"

while [ "$SEC" -le "0" -a "$MIN" -gt "0" -a "$HOUR"  -ge "0"  ]

while [ "$SEC" -le "0" -a "$MIN" -gt "0" -a "$HOUR"  -ge "0"  ]

do

echo -ne "After ${MIN}:${SEC}s is end"

echo -ne "\r  \r"

(($HOUR--))

MIN=60

done

((MIN--))

SEC=60

done

Done

2.vim connection.sh

#!/bin/bash

for NUM  in {1..30}

do

ping -c1 -w1 172.25.254.$NUM &> /dev/null && (

/mnt/ping.exp 172.25.254.$NUM redhat echo '#!/bin/bash' > userfile ec    ho CKUSER='gentent passwd westos$NUM' >>userfile echo '[ -z "$CKUSER"]&&(user    add westos$NUM ' >> userfile echo 'echo 172.25.254.$NUM | passwd --stdin weto    s$NUM) || echo "westos$NUM exist!"'

) || echo 172.25.254.$NUM is connected faild

done

3.vim check.exp

#!/usr/bin/expect

set timeout 3

set Ip      [lindex $argv 0]

set Pass      [lindex $argv 1]

set comn      [lindex $argv 2]

spawn  ssh root@$ip $comn

expect {

"yes/no"

{send"yes/r";exp_continue}

"password:"

{send "$Pass\r"}

}

expect eof

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值