且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

csh goto case bash case

更新时间:2022-09-27 17:38:45

#!/bin/csh
stargoto:
echo "Please input the letter\!"
set letter = $<
switch ( $letter )
case a:
echo "you type A"
breaksw
case b:
echo "you type B"
breaksw
case c:
echo "you type C"
breaksw
case *: #or you can use --[default:] replace [case *:]
goto stargoto
endsw

#!/bin/bash
time=`date|awk '{print $5}'|awk -F 时 '{print $1}'`
#echo "please input the time!"
#read time
#until [ $time -ge 24 ]
#do
        case "$time" in
        [0-9] | 1[0-1]) echo "Good morning!"
        ;;
        12) echo "Lunch time."
        ;;
        1[3-7]) echo "right now is Siesta time.plesase shutdown the computer!"
        shutdown -h now
        ;;
        *) echo "Good night."
        ;;
        esac
#       time=`expr $time + 1`
:
#done


#!/bin/csh -f
startover:
echo "What was your grade?"
set grade = $<
if ( "$grade" < 0 || "$grade" >100 ) then
        echo "Illegal grade\!"
        goto startover
else if ( $grade >= 89 ) then
        echo "A for the genius\!"
else if ( $grade >= 79 ) then
        echo "very good"
else
        echo "shit"
endif

csh相关说明-f参数是用来阻止csh加载.cshrc文件,默认csh执行先加载.cshrc文件,对于MACOS来说应该不存在这个问题,tcsh也是用-f来阻止脚本加载.tcshrc因为加载此文件可能会影响你的脚本变量如果你在此文件设置了相关变量如下所示
#!/bin/csh
#dare
#^r^t
history
cat file1 file2 file3
vi !:1 #vi file1
vi !:2 #vi file2
echo a b c d
echo !$# echo the last 
echo a b c d
echo !*#echo all 
!!:p#echo last you do


#!/bin/bash
while true;do
echo  Are you ready to move on\?
read answer
if [ "$answer" = Y -o "$answer" = y ]
then
break
elif [ "$answer" = N -o "$answer" = n ]
then
echo "you do nothing!"
break
else
:
fi
done
echo "Here wa are!"


#!/bin/csh
ls f{c,oo,.c,b[3-5]}

#list  accord file

#!/bin/bash
if [ $# -lt 1 ]
then
        echo "Usage:$0 filename " >&2
        exit 1
fi
count=1
cat $1 |while read line
do
        [ $count -eq 1 ] && echo "Processing file $1..." > /dev/tty
        echo $count $line
        count=`expr $count + 1`
done > tmp$$
mv tmp$$ $1

#number the file and modify it


#!/bin/bash
set name=root
mail $name << EOF
Hello there,$name
The hour is now `date +%H`
EOF

#here bash test


csh里面的!是一个变量如要输出!须转义


创建目录如果不存则创建存在打印错误退出
参照http://blog.csdn.net/jiazhen/archive/2009/04/28/4133847.aspx此文
修正了一下打印错误信息的问题
#! /bin/bash

is_directory()#此函数用来判断目录是否存在并返回值 
{
  DIR_NAME=$1
  if [ ! -d $DIR_NAME ]; then
    return 1
  else
    return 0
  fi
}

for DIR in "$@"; do#"$@"接收用户输入参数,并以空格为界线,如a b 则是创建a和b
  if is_directory "$DIR"#传递接收到的参数给is_directory判断并返回值
  then
#       echo $?
        if [ $? -eq 0 ]#判断返回值是否为0为0则输出目录存在不为0继续else语句
        then
        echo "Cannot create directory the directory $DIR exist!"
        exit 1
        fi

  else#不为0则执行该语句
    echo "$DIR doesn't exist. Creating it now..."
    mkdir $DIR > /dev/null 2>&1
  fi
done





下面是一个sed脚本用于执行一个sed脚本文件
#!/bin/sh
for x
do
echo "editing $x: \c"  #读取变量x打印换行但\c在这不起作用
if test "$x" = sedscr; then #判断x是否为sedscr
echo "not editing sedscript !"#xsedscr时输出此句
elif test -s $x; then        #xsedscr,判断文件是否为空,为空执行第三个else语句打印出为空
sed -f sedscr $x > /tmp/$x$$ #非空时对x文件执行sedscr里的sed脚本命令,把输出重定向到/tmp/目录下生成x$$文件 $$为执行脚时的pid
if test -s /tmp/$x$$ #判断生成x$$文件是否为空
then
if cmp -s $x /tmp/$x$$ #x$$非空执行cmp命令比较两文件
then
echo "file not changed: \c" #cmp比较两文件是否一样,一样时打印出未改变提示信息
else mv $x $x.bak  #cmp比较后不一样执行该语句
cp /tmp/$x$$ $x
fi
echo "done"
else
echo "Sed produced an empty file\c" x$$为空时打印出该语句
echo " - check your sedscript."
fi
rm -f /tmp/$x$$
else
echo "original file is empty."  #如x参数为空打印该语句
fi
done
echo "all done"


通过学习发现有不少命令类似如上面脚本里的cmp与diff,tr与sed不过些功能tr实现不了sed的功能


if [ $# -lt "3" ]
then
echo Usage:gres pattern replacement file
exit 1;
fi
if [ -f $3 ]
else
echo $3 is not a file
exit 1
fi
A="`echo | tr '\012' '\001'`" #See footnote[11]\012是代表这些是八进制的ASCII A 是用当作分隔符用,为避免文件中有字符是分隔符
sed -e "s$A$1$A$2$A" $3
此脚本参照awk&sed的书稍作修改,主要功能是实现字符串替换

本文转自 qwjhq 51CTO博客,原文链接:http://blog.51cto.com/bingdian/176289