shell编程
文章平均质量分 52
Phoenix_99
这个作者很懒,什么都没留下…
展开
-
自动导入imp数据
date >> start-date2.logsqlplus nciclccd_e/nciclccd_e <<EOFdrop table PARTNERINFO;drop table BKIMP;drop table BKINFO;exitEOFimp nciclccd_e/nciclccd_e file=par_bk.imp log=par_bk.log f...原创 2019-07-09 21:08:40 · 260 阅读 · 0 评论 -
创建用户组,然后创建30个用户个用户目录,并赋予相应权限
#!/bin/bash i=1 groupadd testgroup while [ $i -le 30 ];do if [ $i -le 9 ];then USENAME=STU0${i} else USENAME=STU${i} fi useradd $USENAME mkdir /home/$USENAME chown $USEN原创 2012-02-10 10:31:57 · 985 阅读 · 0 评论 -
shell实例 ----判断输入的用户名是否存在与/etc/passwd中
#!/bin/bashif [ $# -le 0 ]; then echo "Please input the right arg"else i=`cat /etc/passwd | cut -f1 -d':' | grep -w "$1" -c` if [ $i -le 0 ]; then echo "User $1 is not in th passwd"原创 2012-02-10 10:12:18 · 6827 阅读 · 4 评论 -
Here Document (二)
1.生成另外一个脚本的脚本#! /bin/shOUTFILE=generated.sh(cat echo "This is a generated shell script." echo "Generated file will be named: $OUTFILE"a=7b=3let " c = $a * $b "echo " c= $c "exit 0EOF) > $OUTFILEif [ -f "$OUTFILE" ]then chmod 755 $OUTFILEelse echo "Probl原创 2011-04-04 20:19:00 · 478 阅读 · 0 评论 -
Here Document
<br />1.广播,将信息发给所有用户<br />#! /bin/sh<br />wall << zzzzzzzzzzz23End<br />asdfsdfsdaf<br />E-mail yousdf<br />asdfsadfasdf<br />asdfdsf<br />zzzzzzzzzzz23End<br />exit 0<br /> <br />2.虚拟一个2行的虚拟文件<br />#! /bin/sh<br />E_BADARGS=65<br />if [ -z "$1" ]<br />the原创 2011-04-02 00:04:00 · 602 阅读 · 0 评论 -
代码快重定向
<br />1.while循环重定向<br />#! /bin/sh<br />if [ -z "$1" ]<br />then<br /> Filename=yjg.txt<br />else<br /> Filename=$1<br />fi<br />count=0<br />echo<br />while [ "$name" != Smith ]<br />do<br /> read name<br /> echo $name<br /> let "count += 1"<br />done < "原创 2011-03-31 22:15:00 · 454 阅读 · 0 评论 -
exec
1.使用exec重定向stdin#! /bin/shexec 6exec read a1 -------读取文件第一行 read a2 -------读取文件第二行 read a3echoecho "Following lines read from file."echo "------------------------------"echo $a1echo $a2echo $a3echo;echo;echoe原创 2011-03-30 22:52:00 · 829 阅读 · 0 评论 -
shell实例(二十一) ----stty,命令
1.设置一个擦除字符#! /bin/shecho -n "What is your name? "read nameecho "Your name is $name"stty erase '#' ----设置擦除字符为'#'echo -n "What is your name?"read nameecho "Your name is $name."exit 02.关闭终端对密码的echo#! /bin/shechoecho -n "原创 2011-03-19 13:44:00 · 1397 阅读 · 0 评论 -
shell实例(二十) ---seq,getopt,dd,mcookie,m4命令
<br />1.使用seq命令产生循环参数<br />#! /bin/sh<br />echo<br />for a in `seq 80`<br />do<br /> echo -n "$a "<br />done<br />echo;echo<br />COUNT=80<br />for a in `seq $COUNT`<br />do<br /> echo -n "$a "<br />done<br />echo;echo<br />BEGIN=75<br />END=80<br />for a原创 2011-03-18 18:51:00 · 1944 阅读 · 0 评论 -
shell实例(十九) ---nl,bc命令
1.一个计算自己行号的脚本#! /bin/shnl `basename $0`cat -n `basename $0`exit 02.按月偿还贷款#! /bin/shechoecho "Given the principal, interset rate, and term of a mortgage,"echo "Calculate the monthly payment."bottom=1.0echoecho -n "Enter principal (no commas) "read principal原创 2011-03-17 20:15:00 · 4402 阅读 · 0 评论 -
shell实例(十八) ----look,tr命令
1.检查列表中单词的正确性#! /bin/shfile=11-10.shi=0while [ "$word" != "exit 0" ]do let " i += 1" read word echo "/"word/" is "$word"." look $word > /dev/null lookup=$? if [ "$lookup" -eq 0 ] then echo "/"$word/" is valid." else echo "/"$word/" is inval原创 2011-03-17 00:08:00 · 894 阅读 · 0 评论 -
shell实例(十七) ----find和xargs命令
1.分析单词出现的频率#! /bin/shARGS=1E_BADARGS=65E_NOFILE=66if [ $# -ne "$ARGS" ]then echo "Usage: `basename $0` filename" exit $E_BADARGSfiif [ ! -f "$1" ]then echo "File /"$1/" does not exist." exit $E_NOFILEfised -e 's//.//g' -e 's//,//g' -e 's/ ///g' "$1" | t原创 2011-03-15 19:28:00 · 912 阅读 · 0 评论 -
shell实例(十六) ----find和xargs命令
1.删除当前目录下包含特俗字符和空格的文件 #! /bin/shfor filename in *do badname=`echo "$filename" | sed -n '/[+{;"/=?~()&*|$]/p'` rm $badname 2 > /dev/nulldonefind . -name "* *" -exec rm -f {} /;exit 02.原创 2011-03-14 23:32:00 · 940 阅读 · 0 评论 -
shell实例(十五) ----exec命令
1.一个exec自身的脚本#! /bin/shechoecho "This line appears ONCE in the script,yet it keeps echoing."echo "The PID of this instance of the script is still $$."echo "====================Hit Contrl + C to exit ================="sleep 2exec $0echo "This line will neve原创 2011-03-13 22:00:00 · 892 阅读 · 0 评论 -
读取文件行
1.echofile.sh #! /bin/bashFile=passwd{ read line1 read line2} echo "First line in $File is:"echo "$line1"echoecho "Second line in $File is:"echo "$line2"exit 0 2.原创 2012-03-09 17:52:13 · 371 阅读 · 0 评论 -
判断rpm是否可以安装
#! /bin/bash# rpm file checkSUCCESS=0E_NOARGS=65if [ -z "$1" ]; then echo "Usage: `basename $0` rpm-file" exit $E_NOARGSfi{ echo echo "Archive Description:" rpm-qpi $1 ec原创 2012-03-13 12:01:21 · 645 阅读 · 0 评论 -
while循环退出
#! /bin/bashwhile [ "$var"!="end" ]do echo -n "please input a number:" read var if [ "$var" = "end" ] then break #echo "var is $var" #echo "finish" fi echo "var is原创 2012-04-05 13:12:02 · 481 阅读 · 0 评论 -
文件打包归档
#! /bin/bashcd /home/tomcat/inimsbms/inimsfiles/i=2016cur_year=`date +%Y`echo $cur_yearwhile [ $i -le $cur_year ]do echo $i if ls -l *"$i"*.D >/dev/null then if [ $? -eq 0 ]...原创 2019-04-22 15:07:59 · 173 阅读 · 0 评论 -
文件自动上传到FtP(shell)
#! /bin/bashHOST=10.14.96.14FTP_USERNAME=INVFTPGO03FTP_PW=INVFTPGO03P#HOST=192.168.128.3#FTP_USERNAME=bmsncic#FTP_PW=bmsncicDATE=$(date +%Y%m%d)OFILE=OBMS_DIPC_PRICE_$DATE.DKFILE原创 2015-08-03 13:55:54 · 767 阅读 · 0 评论 -
变更赋值
#! /bin/basha=0for name in *.*do b=$(ls -l $name | awk '{print $5}') if test $b -ge $a then a=$b namemax=$name fi doneecho "The max size file is $namemax"原创 2012-04-12 13:16:49 · 318 阅读 · 0 评论 -
gzip循环压缩文件
#! /bin/bashfor i in ` ls -l *.txt | awk '{ print $8 }' `do gzip $idoneecho "gzip OK"原创 2012-04-11 17:19:54 · 896 阅读 · 0 评论 -
简单函数
#! /bin/bashp_num(){ num=$1 echo $num}for n in $@do p_num $ndone原创 2012-04-12 11:13:02 · 292 阅读 · 0 评论 -
删除文件大小为0的文件
#! /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原创 2012-04-11 15:38:06 · 1645 阅读 · 0 评论 -
case用法
echo "a is 5, b is 3. Please select you method"a=5b=3select c in "a+b" "a-b" "a*b" "a/b"; do breakdonecase $c in "a+b") echo "$a+$b"= ` expr $a + $b `;;"a-b") echo "$a-$b"= ` expr $a原创 2012-04-10 17:09:33 · 556 阅读 · 0 评论 -
解压缩文件
#! /bin/bashftype=`file "$1"`case "$ftype" in "$1: Zip archive"*) unzip "$1" ;; "$1: gzip compressed"*) gunzip "$1" ;; "$1: bzip2 compressed"*) bunzip2 "$1" ;; *)原创 2012-04-09 17:19:54 · 524 阅读 · 0 评论 -
select用法
#! /bin/bashecho "What is your favourite OS?"select var in "Linux" "UNIX" "Windows" "Other";do echo "You have selected $var."done原创 2012-04-05 15:02:18 · 459 阅读 · 0 评论 -
shift应用,求参数之和
#! /bin/bashresult=1while [ $# -gt 0 ]do result=`expr $result \* $1` shiftdone echo "result is $result"原创 2012-04-05 14:29:59 · 334 阅读 · 0 评论 -
100之内除以3的余数之和
#! /bin/bashtotal=0for i in `seq 1 100`do if [ `expr $i % 3` = 0 ] then total=`expr $total + $i` fidoneecho "total is $total"原创 2012-04-05 14:08:48 · 1122 阅读 · 0 评论 -
数据乘积
#! /bin/bashfor i in `seq 1 10`do result=` expr $i \* $i ` echo " $i ------ $result "done原创 2012-04-05 13:11:02 · 368 阅读 · 0 评论 -
linux shell编程实例总结
1.查找当前目录中所有大于500M的文件,把这些文件名写到一个文本文件中,并统计其个数。find ./ -size +500M -type f | tee file_list | wc -l2.在目录/tmp下找到100个以abc开头的文件,然后把这些文件的第一行保存到文件new中。for filename in `find /tmp -type f -name "abc*"|he转载 2012-04-05 16:45:18 · 4589 阅读 · 1 评论 -
shell实例(十三) ----pushd,popd,let命令,eval命令
<br />1.修改当前目录<br />#! /bin/sh<br />dir1=/usr/local<br />dir2=/var/spool<br />pushd $dir1<br />echo "Now in direcory `pwd`."<br />pushd $dir2<br />echo "Now in directory `pwd`."<br />echo "The top entry in the DIRSTACT array is $DIRSTACK."<br />popd<br />e原创 2011-03-13 01:08:00 · 1701 阅读 · 0 评论 -
shell实例(十四) ----set,export,getopts命令
1.使用set命令来改变脚本的位置参数#! /bin/shechoecho "Positional parameters before set /`uname -a/` :"echo "Command-line argument #1 = $1"echo "Command-line argument #2 = $2"echo "Command-line argument #3 = $3"set `uname -a`echo $echo "Positional parameters after set /`u原创 2011-03-13 16:07:00 · 1083 阅读 · 0 评论 -
shell实例(十二) ----内部命令和内建命令(printf,read)
<br />说明:read -s -n1 -p:<br /> -s:意味着不打印输出<br /> -n: N:只接受N个字符<br /> -p:读取输入之前打印出后边的提示符<br />1.一个fork出多个自身实例的脚本<br />#! /bin/sh<br />PIDS=$(pidof sh $0) -----$()相当于两个反引号(``),pidof sh $0:查找出sh 和 $0(脚本本身)相关的pid<br />P_arry=( $PIDS )<br />echo $PIDS<br /原创 2011-03-10 16:41:00 · 1227 阅读 · 0 评论 -
<<用法,shell工具
<br />1.快速创建文件<br />cat >> myfile.txt <<<br /> yjg.txt<br /> zhanghong.txt<br /> kkk<br /> <br />2.快速创建打印文档<br /> lpr << KK<br /> asdfsd<br />asdf<br />sdf<br />sadf<br />KK<br /> <br />3.自动选择菜单<br />usr/local/sybin/syb_backup >> $log_f 2>&1 << MAYD原创 2010-12-25 23:37:00 · 1454 阅读 · 0 评论 -
嵌入命令
<br />1.嵌入命令列表<br />: 空,永远返回为t r u e<br />. 从当前s h e l l中执行操作<br />b r e a k 退出f o r、w h i l e、u n t i l或c a s e语句<br />c d原创 2010-12-24 22:33:00 · 472 阅读 · 0 评论 -
几个脚本
1.pingall#!/bin/sh#pingallcat /etc/hosts | grep -v '^#' | while read LINEdo ADDR=` awk '{print $1}' ` for MACHINE in $ADDR do ping -s -cl $MACHINE donedone2.去除空行sed '/^$/d' zh.txt3原创 2010-12-26 00:45:00 · 516 阅读 · 0 评论 -
shift和getopts
<br />1.shift命令<br />#!/bin/sh<br />#opt2.sh<br />loop=0<br />while [ $# -ne 0 ]<br />do<br /> echo $1<br /> shift<br />done<br /> <br />结果:<br />[root@localhost yjg]# ./opt2.sh 1 2 3 4 5<br />1<br />2<br />3<br />4<br />5<br />[root@localhost yjg]# <br原创 2010-12-23 22:54:00 · 542 阅读 · 0 评论 -
shell函数
1.在脚本中定义函数 函数有两部分组成:函数名,函数体2.建立和执行函数function findit () { if [ $# -lt 1 ]; then echo "Uage : findit file"; return 1; fi; for loop in "$@"; do find /usr/local/yjg $loop -print; done}[root@localhost yjg]# finditUage : find原创 2010-12-21 18:04:00 · 591 阅读 · 0 评论 -
环境和shell变量
1.变量结合 [root@localhost ~]# ERROR_MSG="Sorry this file doet not exist user $LOGNAME" [root@localhost ~]# echo ${ERROR_MSG} Sorry this file doet not exist user root [root@localhost ~]# echo ${FIRTST}${SURNAME} Bruce Willis2.测试变量是否设置 1.[root@localhost原创 2010-12-14 00:07:00 · 431 阅读 · 0 评论 -
控制流
1.条件测试 -d ---目录 -f ---正规文件 -L ----符号链接 -r ----可读 -s ----文件长度大于0,非空 -w ---可写 -u ---文件有suid位位置-x ----可执行[root@localhost yjg]# test -x yjg.txt[root@localhost yjg]# echo $?0原创 2010-12-14 22:03:00 · 340 阅读 · 0 评论