bash
来源:互联网 发布:png转ico软件 编辑:程序博客网 时间:2024/06/06 05:50
转自http://blog.csdn.net/jb19900111/article/details/13507815
自己写了一下小的shell实例,虽然很小,但所有的大的程序都是由小的模块堆积起来的,程序员一定要懂得一种脚本的书写,而我,只会在Linux下工作,所以就只能写linux的shell脚本了,呵呵,本文会陆续更新,给自己加油!
1.模拟linnux登录shell
- #!/bin/bash ##! /bin/sh 是指此脚本使用/bin/sh来解释执行,#!是特殊的表示符,其后面根的是此解释此脚本的shell的路径。
- echo -n “login:” #echo -n 不换行输出
- read name #read命令接收标准输入(键盘)的输入,或者其他文件描述符的输入。得到输入后,read命令将数据放入一个标准变量中。
- #在上面read后面的变量只有name一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据给第二个变量。如果输入数据个数过多,则最后剩下的所有值都给最后一个变量,如果太少输入不会结束。
- echo -n “password:”
- read passwd
- if [ name = "cht" -a passwd = “abc” ];then #使用一个定义过的变量,只要在变量名前面加美元符号($),
=
判断字符串是否相等,[ …. -a ….. ] 相当于 “与” -o 表示 或 - echo “the host and password is right!”
- else echo “input is error!”
- fi
- #!/bin/bash
- echo -n “login:”
- read name
- echo -n “password:”
- read passwd
- if [ name = "cht" -a passwd = “abc” ];then
- echo “the host and password is right!”
- else echo “input is error!”
- fi
#!/bin/bashecho -n "login:" read nameecho -n "password:"read passwdif [ $name = "cht" -a $passwd = "abc" ];thenecho "the host and password is right!"else echo "input is error!"fi
2.比较两个数大小
- #!/bin/bash
- echo “please enter two number”
- read a
- read b
- if test a -eq b #test 整数1 –eq 整数2 整数相等
- then echo “NO.1 = NO.2”
- elif test a -gt b #test 整数1 –gt 整数2 整数1大于整数2
- then echo “NO.1 > NO.2”
- else echo “NO.1 < NO.2”
- fi
- #!/bin/bash
- echo “please enter two number”
- read a
- read b
- if test a -eq b
- then echo “NO.1 = NO.2”
- elif test a -gt b
- then echo “NO.1 > NO.2”
- else echo “NO.1 < NO.2”
- fi
#!/bin/bashecho "please enter two number"read aread bif test $a -eq $bthen echo "NO.1 = NO.2"elif test $a -gt $bthen echo "NO.1 > NO.2"else echo "NO.1 < NO.2" fi
3.查找/root/目录下是否存在该文件
- #!/bin/bash
- echo “enter a file name:”
- read a
- if test -e /root/$a #test –e File 文件是否存在 (常用)
- then echo “the file is exist!”
- else echo “the file is not exist!”
- fi
- #!/bin/bash
- echo “enter a file name:”
- read a
- if test -e /root/$a
- then echo “the file is exist!”
- else echo “the file is not exist!”
- fi
#!/bin/bashecho "enter a file name:"read aif test -e /root/$a then echo "the file is exist!"else echo "the file is not exist!"fi
4.for循环的使用
- #!/bin/bash
- clear #刷新屏幕
- for num in 1 2 3 4 5 6 7 8 9 10 #列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量num
- do
- echo ”$num”
- done
- #!/bin/bash
- clear
- for num in 1 2 3 4 5 6 7 8 9 10
- do
- echo ”$num”
- done
#!/bin/bashclearfor num in 1 2 3 4 5 6 7 8 9 10do echo "$num"done
5..查看是否当前用户
- #!/bin/bash
- echo “Please enter a user:”
- read a
- b=(whoami) #使用一个定义过的变量,只要在变量名前面加美元符号()即可
- if test a = b #test 字符串1=字符串2 字符串是否相等,若相等返回true
- then echo “the user is running.”
- else echo “the user is not running.”
- fi
- #!/bin/bash
- echo “Please enter a user:”
- read a
- b=(whoami) </span></li><li class="alt"><span>if test a = $b
- then echo “the user is running.”
- else echo “the user is not running.”
- fi
#!/bin/bashecho "Please enter a user:"read ab=$(whoami)if test $a = $bthen echo "the user is running."else echo "the user is not running."fi
6.删除当前目录下大小为0的文件
- #!/bin/bash
- for filename in `ls`
- do
- if test -d filename #<span style="font-family:'microsoft yahei'; line-height:35px">test –d File 文件并且是目录 </span></span></li><li class="alt"> then b=0 </li><li> else </li><li class="alt"> <span style="color:#ff0000"> a=(ls -l filename | awk '{ print $5 }') </span></li><li> <span style="font-size:10px"> if test a -eq 0 #test 整数1 –eq 整数2 整数相等
- then rm $filename
- fi
- fi
- done
- #!/bin/bash
- for filename in `ls`
- do
- if test -d filename </span></li><li class="alt"><span> then b=0 </span></li><li class=""><span> else </span></li><li class="alt"><span> a=(ls -l filename | awk '{ print $5 }') </span></li><li class=""><span> if test a -eq 0
- then rm $filename
- fi
- fi
- done
#!/bin/bashfor filename in `ls`do if test -d $filename then b=0 else a=$(ls -l $filename | awk '{ print $5 }') if test $a -eq 0 then rm $filename fi fi done
7.如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G
- #!/bin/bash
- while line=`ls /export/um_lpp_source` //把目录/export/um_lpp_source下的文件赋给line变量
- do
- if test line="<span style="font-size:10px">" #<span style="color:rgb(85,85,85); font-family:'microsoft yahei'; line-height:35px">if test (表达式为真) </span></span></li><li class="alt"> then echo "NULL" #输出字符串NULL</li><li> sleep 1<span style="font-size:10px"> #<em>sleep 1</em> 睡眠1秒</span></li><li class="alt"> else echo line
- chfs -a size=3G /export/um_lpp_source #把/export/um_lpp_source的大小改为3G
- exit 0
- fi
- done
- #!/bin/bash
- while line=`ls /export/um_lpp_source`
- do
- if test line="" </span></li><li class="alt"><span> then echo "NULL" </span></li><li class=""><span> sleep 1 </span></li><li class="alt"><span> else echo line
- chfs -a size=3G /export/um_lpp_source
- exit 0
- fi
- done
#!/bin/bashwhile line=`ls /export/um_lpp_source`do if test $line="" then echo "NULL" sleep 1 else echo $line chfs -a size=3G /export/um_lpp_source exit 0 fidone
8.测试IP地址
- #!/bin/bash
- for i in 1 2 3 4 5 6 7 8 9
- do
- echo “the number of i computer is " </li><li class="alt"> ping -c 1 192.168.0.i #-c指定要求完成的回应次数
- done
- #!/bin/bash
- for i in 1 2 3 4 5 6 7 8 9
- do
- echo “the number of i computer is " </span></li><li class="alt"><span> ping -c 1 192.168.0.i
- done
#!/bin/bashfor i in 1 2 3 4 5 6 7 8 9 do echo "the number of $i computer is " ping -c 1 192.168.0.$idone
9.如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件拷贝到当前目录下
- #!/bin/sh
- a=2
- while name=”test.log”
- do
- sleep 1 #sleep 1 睡眠1秒
- b=(ls -l name | awk ’{print 5}') </li><li class="alt"> if test b -gt $a #test 整数1 –gt 整数2 整数1大于整数2
- then `cp /opt/*.tar.gz .`
- exit 0
- fi
- done
- #!/bin/sh
- a=2
- while name=”test.log”
- do
- sleep 1
- b=(ls -l name | awk ’{print 5}') </span></li><li class="alt"><span> if test b -gt $a
- then `cp /opt/*.tar.gz .`
- exit 0
- fi
- done
#!/bin/sha=2while name="test.log"do sleep 1 b=$(ls -l $name | awk '{print $5}') if test $b -gt $a then `cp /opt/*.tar.gz .` exit 0 fidone
10.打印读取的内容,为下面的例子做准备
- #!/bin/bash
- while read name
- do
- echo $name
- done
- #!/bin/bash
- while read name
- do
- echo $name
- done
#!/bin/bashwhile read namedoecho $namedone
11.从0.sh中读取内容并打印
- #!/bin/bash
- while read line
- do
- echo $line
- done < 0.sh
- #!/bin/bash
- while read line
- do
- echo $line
- done < 0.sh
#!/bin/bashwhile read linedo echo $linedone < 0.sh12.读取a.c中的内容并做加1运算
- #!/bin/bash
- test -e a.c #test –e File 文件是否存在 (常用)
- while read line
- do
- a=
(( line+1)) #内容做加1运算 - done < a.c
- echo $a
- #!/bin/bash
- test -e a.c
- while read line
- do
- a=
(( line+1)) - done < a.c
- echo $a
#!/bin/bashtest -e a.cwhile read linedo a=$(($line+1))done < a.cecho $a
13.普通无参数函数
- #!/bin/bash
- p ()
- {
- echo “hello”
- }
- p
- #!/bin/bash
- p ()
- {
- echo “hello”
- }
- p
#!/bin/bashp (){ echo "hello"}p
14.给函数传递参数
- #!/bin/bash
- p_num ()
- {
- num=1 </li><li class="alt"> echo num
- }
- for n in @ #输入参数到函数中</li><li>do </li><li class="alt"> p_num n
- done
- #!/bin/bash
- p_num ()
- {
- num=1 </span></li><li class="alt"><span> echo num
- }
- for n in @ </span></li><li class=""><span>do </span></li><li class="alt"><span> p_num n
- done
#!/bin/bashp_num (){ num=$1 echo $num}for n in $@do p_num $ndone
15.创建文件夹
- #!/bin/bash
- while :
- do
- echo “please input file’s name:”
- read a
- if test -e /root/a <span style="font-size:10px">#<span style="color:rgb(85,85,85); font-family:'microsoft yahei'; line-height:35px">test –e File 文件是否存在 (常用)</span></span></li><li class="alt"> then </li><li> echo "the file is existing Please input new file name:" </li><li class="alt"> else </li><li> mkdir a
- echo “you aye sussesful!”
- break
- fi
- done
- #!/bin/bash
- while :
- do
- echo “please input file’s name:”
- read a
- if test -e /root/a </span></li><li class="alt"><span> then </span></li><li class=""><span> echo "the file is existing Please input new file name:" </span></li><li class="alt"><span> else </span></li><li class=""><span> mkdir a
- echo “you aye sussesful!”
- break
- fi
- done
#!/bin/bashwhile :do echo "please input file's name:" read a if test -e /root/$a then echo "the file is existing Please input new file name:" else mkdir $a echo "you aye sussesful!" break fidone
16.获取本机IP地址
- #!/bin/bash
- ifconfig | grep “inet addr:” | awk ’{ print $2 }’| sed ‘s/addr://g’
- #!/bin/bash
- ifconfig | grep “inet addr:” | awk ’{ print $2 }’| sed ‘s/addr://g’
#!/bin/bashifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'
17.查找最大文件
- #!/bin/bash
- a=0
- for name in *.*
- do
- b=(ls -l name | awk ’{print 5}') </li><li> if test b -gt a <span style="font-size:10px">#</span><span style="font-size:10px">test 整数1 –gt 整数2 整数1大于整数2 </span></li><li class="alt"> then a=b #更改比较值
- namemax=name </li><li class="alt"> fi </li><li>done </li><li class="alt">echo "the max file is namemax”
- #!/bin/bash
- a=0
- for name in *.*
- do
- b=(ls -l name | awk ’{print 5}') </span></li><li class=""><span> if test b -gt a </span></li><li class="alt"><span> then a=b
- namemax=name </span></li><li class="alt"><span> fi </span></li><li class=""><span>done </span></li><li class="alt"><span>echo "the max file is namemax”
#!/bin/basha=0for name in *.*do b=$(ls -l $name | awk '{print $5}') if test $b -gt $a then a=$b namemax=$name fidoneecho "the max file is $namemax"
18.查找当前网段内IP用户,重定向到ip.txt文件中
- #!/bin/bash
- a=1
- while :
- do
- a=
(( a+1)) #从1开始不断地向上加,直到等于255时退出 - if test a -gt 255 <span style="font-size:10px">#test 整数1 –gt 整数2 整数1大于整数2 </span></li><li class="alt"> then break </li><li> else </li><li class="alt"> echo (ping -c 1 192.168.0.a | grep "ttl" | awk '{print $4}'| sed 's/://g') #-c 后面带发送次数 ,sed 's/://g'是吧空格全部替换为\</li><li> ip=(ping -c 1 192.168.0.a | grep "ttl" | awk '{print $4}'| sed 's/://g') </li><li class="alt"> echo ip >> ip.txt
- fi
- done
TTL,存活时间TTL是IP协议包中的一个值,它告诉网络路由器包在网络中的时间是否太长而应被丢
- #!/bin/bash
- a=1
- while :
- do
- a=
(( a+1)) - if test a -gt 255 </span></li><li class="alt"><span> then break </span></li><li class=""><span> else </span></li><li class="alt"><span> echo (ping -c 1 192.168.0.a | grep "ttl" | awk '{print $4}'| sed 's/://g') </span></li><li class=""><span> ip=(ping -c 1 192.168.0.a | grep "ttl" | awk '{print $4}'| sed 's/://g') </span></li><li class="alt"><span> echo ip >> ip.txt
- fi
- done
#!/bin/basha=1while :do a=$(($a+1)) if test $a -gt 255 then break else echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') echo $ip >> ip.txt fidone
19.打印当前用户
- #!/bin/bash
- echo “Current User is :”
- echo (ps | grep "" | awk '{print 2}’) #ps显示瞬间行程 (process) 的动态,</em>是当前进程号,就是你的shell的进程号</li></ol>” | awk ’{print $2}’)
<div class="save_code tracking-ad" style=""><a target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt=""></a></div>
<div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
<div class="dp-highlighter bg_plain"><div class="bar"><div class="tools"><b>[plain]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 185px; top: 9618px; width: 15px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_38" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="15" height="16" name="ZeroClipboardMovie_38" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=38&width=15&height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1"><li class="alt"><span><span>#!/bin/bash </span></span></li><li class=""><span>echo "Current User is :" </span></li><li class="alt"><span>echo $(ps | grep "
#!/bin/bashecho "Current User is :"echo $(ps | grep "$$" | awk '{print $2}')
20.case语句练习
- #!/bin/bash
- clear
- echo “enter a number from 1 to 5:”
- read num
- case $num in
- 1) echo “you enter 1”
- ;;
- 2) echo “you enter 2”
- ;;
- 3) echo “you enter 3”
- ;;
- 4) echo “you enter 4”
- ;;
- 5) echo “you enter 5”
- ;;
- *) echo “error” #如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令
- ;;
- esac
- #!/bin/bash
- clear
- echo “enter a number from 1 to 5:”
- read num
- case $num in
- 1) echo “you enter 1”
- ;;
- 2) echo “you enter 2”
- ;;
- 3) echo “you enter 3”
- ;;
- 4) echo “you enter 4”
- ;;
- 5) echo “you enter 5”
- ;;
- *) echo “error”
- ;;
- esac
#!/bin/bashclearecho "enter a number from 1 to 5:"read numcase $num in 1) echo "you enter 1" ;; 2) echo "you enter 2" ;; 3) echo "you enter 3" ;; 4) echo "you enter 4" ;; 5) echo "you enter 5" ;; *) echo "error" ;;esac
21.yes/no返回不同的结构
- #!/bin/bash
- clear
- echo “enter [y/n]:”
- read a
- case a in #调用变量a</li><li> y|Y|Yes|YES) echo "you enter a”
- ;; #;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
- n|N|NO|no) echo “you enter $a”
- ;;
- *) echo “error”
- ;;
- esac
- #!/bin/bash
- clear
- echo “enter [y/n]:”
- read a
- case a in </span></li><li class=""><span> y|Y|Yes|YES) echo "you enter a”
- ;;
- n|N|NO|no) echo “you enter $a”
- ;;
- *) echo “error”
- ;;
- esac
#!/bin/bashclearecho "enter [y/n]:"read acase $a in y|Y|Yes|YES) echo "you enter $a" ;; n|N|NO|no) echo "you enter $a" ;; *) echo "error" ;;esac
22.内置命令的使用
- #!/bin/bash
- clear #清除终端屏幕
- echo “Hello, USER" #USER是环境变量
- echo “Today ‘s date id `date`” #显示命令date
- echo “the user is :”
- who #who命令能够打印 当前都有谁登录到系统中 的相关信息
- echo “this is `uname -s`” #uname -s`打印当前系统相关信息,-s或–sysname:显示操作系统名称
- echo “that’s all folks! ”
- #!/bin/bash
- clear
- echo “Hello, $USER”
- echo “Today ‘s date id `date`”
- echo “the user is :”
- who
- echo “this is `uname -s`”
- echo “that’s all folks! ”
#!/bin/bashclearecho "Hello, $USER"echo "Today 's date id `date`"echo "the user is :"whoecho "this is `uname -s`"echo "that's all folks! "
23.打印无密码用户
- #!/bin/bash
- echo “No Password User are :”
- echo (cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print 1}’) #BEGIN{ 这里面放的是执行前的语句 },从也就是第1个参数
阅读全文
0 0
- bash
- .bash
- bash
- bash
- bash
- bash
- bash
- Bash
- Bash
- bash
- bash
- Bash
- bash
- BASH
- bash
- bash
- Bash
- bash
- C# DateTime与时间戳转换
- 博弈论
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题 Overlapping Rectangles(线段树)
- 图像分割算法之 Graph Cut
- 学习之路-抓取策略
- bash
- java基础面试题
- 坦克小作战 js版(未完工)
- 7-29 修理牧场(25 分)
- 进制的转换
- 内核空间与用户空间的通信方式
- js比较器组件Comparator.js
- interleaving-string
- Python初学笔记-io流和异常