######### shell脚本 ######### 很多脚本开头都是这样的: #!/bin/bash ##执行的子环境为 /bin/bash 测试: vimhello.sh ------- #!/bin/bash echo hello world watch -n 1 date ------- 将它放入后台运行 ps f 查看从属关系 [root@localhost tmp]# ps f PID TTY STAT TIME COMMAND 30403 pts/0 Ss 0:00 -bash 30615 pts/0 T 0:00 \_ /bin/bash ./hello.shhello.sh 30616 pts/0 T 0:00 | \_ watch -n 1 date 30631 pts/0 R+ 0:00 \_ ps f 30615 ... \_ /bin/bash ./hello.shhello.sh 能看出来,hello.sh是在/bin/bash下执行的 字符串比较运算符: =、!=、-z、-n -z string ##string长度为0则为真 -n string ##string长度非0则为真 = ##相等 != ##不相等 数字比较运算符: -eq、-ne、-lt、-le、-gt、-ge -eq ##等于 -ne ##不等于 -lt ##小于 -le ##小于等于 -gt ##大于 -ge ##大于等于 文件状态运算符: test-{b|c|e|f|d|r|w|x|s|L|nt|ot} FILE/DIRECTORY b ##二进制文件 c ##字符设备 e ##存在为真 f ##常规文件 d ##目录 r ##可读 w ##可写 x ##可执行 s ##套接字 L ##软链接 nt ##前者比后者新(最后更改时间) ot ##前者比后者旧(最后更改时间) ======= if语句 ======= if [] then else elif then fi --------------------------------------------- #!/bin/bash if [ -z "$1" ] then echo ERROR:you not give me aipaddress else ping -c1 -w1 $1 &>/dev/null&& ( echo $1 is up) || (echo $1 is down) fi ---------------------------------------------- ========= case语句 ========= case "$1" in start) systemctl start $2 ;; stop) systemctl stop $2 ;; reload|restart) systemctl stop $2 systemctl start $2 ;; *) echo "Usage: $0(start|stop|restart|reload)" ;; esac ======== for语句 ======== for ((i=10;i>0;i--)) do done +++++++++++++++++++++ for num in $(seq 1 $usernum) do done +++++++++++++++++++++ for num in {1..30} do done ---------------------------------------------- 题目:现在有两个文件 password与username分别存着密码与用户名,现在写一个脚本,从这两个文件里提取密码与用户名建立用户。 #!/bin/bash usernum=$(wc -l /tmp/username |cut -d " " -f 1) for num in $(seq 1 $usernum) do user=`sed -n ${num}p/tmp/username` passwd=`sed -n ${num}p /tmp/passwd` useradd $user -p $passwd &> /dev/null done 题目:10秒倒计时。 #!/bin/bash for ((i=10;i>0;i--)) do echo -n $i sleep 1 echo -ne '\r \r' done 题目:把系统里所属组为mail的文件拷贝到 /mnt 下并且文件的后缀为当前时间 #!/bin/bash datename=mail.`date +%H-%M-%S` mkdir /mnt/$datename find / -group mail -exec cp -p {} /mnt/$datename \; &> /dev/null & 题目:ping 1..30通不通,通显示ok 不通显示down #!/bin/bash for num in {1..30} do ping -c1 -w1 172.25.254.$num&> /dev/null &&( echo 172.25.254.$num is ok)||( echo 172.25.254.$num isdown) done --------------------------------------------------- ========== while语句 ========== while [ "" = "" ] do done =========== expect语句 =========== 在shell中利用expect实现自动应答脚本。 #!/usr/bin/expect #set timeout 10 ##所有的expect命令等待响应的超时时间为10s spawn ./talk ##选择要应答的文件,此处为 ./talk expect "who" ##判断上次输出结果里是否包含who,有则立即返回下一句send,没有则等待超时时间 send "firefly\n" expect "are youhappy?" send "Yes,I amhappy.\n" expect "why?" send "任性!\n" expect eof ##在输出中搜索文件结束符,若没有这个,脚本立即退出,得不到正确结果 interact ##执行完成保持交互式状态,可以进行手工操作 exit ########## 环境变量 ########## 环境级变量: [root@localhost mnt]# cat test.sh #!/bin/bash echo $a [root@localhost mnt]# a=1 [root@localhost mnt]# echo $a 1 [root@localhost mnt]# sh test.sh [root@localhost mnt]# export a=2 ##在当前shell下可用 [root@localhost mnt]# sh test.sh 2 [root@localhost mnt]#echo $a 2 用户级变量: cd /root ##用户家目录 vim .bash_profile ------ . . PATH=$PATH:$HOME/bin:/mnt ##/mnt下的 .sh 文件也能直接被执行,不用写绝对路径 export PATH export a=1 ##用户级变量$a . . ------- [root@localhost ~]# source.bash_profile 系统级变量: [root@localhost ~]# vim/etc/profile [root@localhost ~]# source/etc/profile ------ . . export a=2 ------ 当系统级变量与用户级变量同时存在时,以后设置的变量为准,重启shell之后,以用户级变量为准 ####### 系统定制 ####### 临时更改你的终端显示: echo $PS1 PS1="LINUX >" 将[root@dns1 ~]# 改为 LINUX > ===== 别名 ===== 使用: alias命令可以用来自定义属于自己的系统命令,写入 ~/.bashrc 文件永久生效 查看别名: alias [root@dns1 ~]# alias alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' 设置别名: alias mycom='echo hello;hostname' 格式: alias 命令名='命令功能实现' 删除别名: unalias mycom