Genesis2000脚本编写常用命令解析

Genesis2000脚本编写常用命令解析

2023年7月10日发(作者:)

Genesis2000脚本编写常用命令解析

1. switch 的用法,注意每一个 case 必须要以 breaksw 结尾

否则会继续执行下一个 case 的命令

(1) 另外, $< 的意思是取得使用者的 stand input

(2) echo 若加上 -n 的选项,则游标会停留在该行最后

echo -n "Input one color: "

set STOPLIGHT = $<

switch ($STOPLIGHT)

case red:

echo "red"

breaksw

case orange:

echo "orange"

breaksw

case green:

echo "green"

breaksw

default:

echo "you input $STOPLIGHT"

endsw

2. 利用 set 来取得变数, set ABC = "I am ABC"

也可以利用 `command` 来取得命令

且外,case 也可以用万用字元 * 来代替

set VER = `uname -r`

switch ($VER)

case 5.5:

echo "run the setup of $VER"

breaksw

case 5.3:

echo "run the setup of $VER"

breaksw

case 5.*:

echo "like 5.x"

breaksw

case 4.*:

echo "like 4.x"

breaksw

default:

echo "no idea"

endsw

3. if 的语法,比较数字

set n1 = 1

set n2 = 2

if ($n1 == $n2) then echo "$n1 Equal $n2"

else

echo "$n1 Not Equal $n2"

endif

4. if 的语法,比较字串

set n1 = abcdef

set n2 = abcde

if ($n1 == $n2) then

echo "$n1 Equal $n2"

else

echo "$n1 Not Equal $n2"

endif

5. if 的语法,比较相似的字串

set n1 = abcdef

set n2 = abcde

if ($n1 =~ $n2) then

echo "$n1 Like $n2"

else

echo "$n1 Not Like $n2"

endif

6. if 的语法,比较数字的大小

set n1 = 1

set n2 = 2

if ($n1 > $n2) then

echo "$n1 > $n2"

else

echo "$n1 < $n2"

endif

7. 每分钟执行一次的程式

# mm 等于当天时间的【分钟】数

set mm = `date | cut -d' ' -f4 | cut -d: -f2`

if ( -r $ ) then

rm $

touch $

else

touch $

endif

while ( $mm <= 16 )

set mm = `date | cut -d' ' -f4 | cut -d: -f2`

echo "$mm now is `date`"

sleep 60

#echo "$mm now is `date`" >> $ end

echo "Over" >> $

8. 一个回圈的范例,并且利用 expr 去作加的动作

回圈的语法如下:

foreach number (1 2 3)

echo $number

end

set counter = 0

while ($counter <= 10)

echo "sleeping for 5 seconds"

sleep 5

counter = `expr $counter + 1 `

end

9. 设定一个用当天月份与日期作为档案名称的程式

如今天是 10/02 , 则 $prefix 会等于 该程式 + 1002

1002

set prefix = `basename $0``date '+ %m%d'`

echo $0

echo $prefix

10. 移除在 foreach 回圈内指定的档案内的 font 字串

foreach file ([b,e,g,h,s]*.html)

echo -n "Processing $file, remove the line number `grep -n font $file`"

# $log 表示这个 $file 有几个 font 字串

set log = `grep -c font $file`

if ( $log == '0' ) then

echo ", pass $file"

else

# 先找出该档案的第一次出现 font 的行数,如果 3,则 $cmd = 3d

set cmd = `grep -n font $file | cut -d: -f1 | head -1`d

# 利用 sed 去执行删除的动作,并把结果输出到 ${file}1

sed $cmd $file > ${file}1

# 如果 ${file}1 没有资料,则 passing

if ( -z ${file}1 ) then

echo " , ${file}1 is zero"

else

cp ${file}1 $file

rm {$file}1

echo " , $file remove ok"

endif

endif

end

# 后来看过 sed 的更进一步用法,发现先前写的太笨了,试试这个

# sed /font/d $file > ${file}1 # 一次 OK, 我真是大笨蛋

11. 功能:将指定的档案中,出现第一次【回】的那一行,加上 xxxx

foreach file (sky*.html)

set filetitle = ftitle

# 主要部份为 sed 部份 s/^ *// 表示将该行第一个字元前的空白删除

echo "`grep 回 $file | head -1 | sed -e 's/^ *//'`" > $ftitle

# 将刚刚那一行,再插回去

head -1 $file > ${file}head

sed 1d $file > ${file}1

cat $ftitle >> ${file}head

cat ${file}1 >> ${file}head

cp ${file}head $file

rm ${file}1

rm $ftitle

rm ${file}head

echo "$file ok"

end

12. 一个实际建立一个 ftp server 的程式

里面包括许多应用,相当有参考价值

( 未完成 )

set path = ( /usr/bin /usr/sbin )

#

set true = `grep -c ftp /etc/passwd`

if ( $true == 0 ) then

echo "no ftp user in your system"

echo -n "do you want to create the ftp user? "

set answer = $<

if ($answer == 'y' || $answer == 'Y') then

set maxid = `sort /etc/passwd | tail -1 | cut -d: -f3`

echo $maxid

set newid = `expr $maxid + 1`

echo $newid

echo "/usr/sbin/useradd -d /home1/ftp -u $newid -s /etc/false ftp"

endif

else

echo "Good. Your system already has the ftp user. "

set ftphome = `grep ftp: /etc/passwd | cut -d: -f6`

echo $ftphome

endif

if ( -z $ftphome ) then

echo "ftphome must be non-null"

exit 2

endif

if ( $ftphome == "/usr" || $ftphome == "/" ) then

echo "ftphome can't be / or /usr"

exit 2

endif

# create the ftp home directory

if ( ! -d $ftphome ) then

echo "mkdir $ftphome"

endif

echo "Setting up the ftphome for SunOS `uname -r`"

if ( ! -d $ftphome ) then

echo "mkdir -p $ftphome/usr/bin"

endif

cp /bin/ls $ftphome/usr/bin

chmod 111 $ftphome/usr/bin/ls

chown root $ftphome/usr/bin

chmod 555 $ftphome/usr/bin

if ( -r $ftphome/bin ) then

mv -f $ftphome/bin $ftphome/Obin

endif

ln -s usr/bin $ftphome

13. 取得该使用者的 UID

if ( $#argv == 0 ) then

echo "$0 usage: $1 username"

exit 2

endif

set uid = `grep $1 /etc/passwd | cut -d: -f3`

echo $uid

14. 将指定档案内的 html 取代成 htm

foreach file ( *.html )

echo "Processing $file ..."

sed s/html/htm/ $file > ${file}1

cp ${file}1 $file

rm ${file}1

end

15. 一个简简单单的范例,看看就好

#!/bin/csh -f

echo .................

echo WELCOME to * TAPE COPY * echo .................

echo Enter your name:

# $< can read from stand input

set name = $<

echo " "

echo Hi $name !

set D = `date`

echo Today's date is $D[1] $D[2] $D[3]

if ($D[1] == Mon) then

echo -------------------------------------------------------------

echo Today is $D[1]day $name, it's time to copy your directorys!

echo -------------------------------------------------------------

else

echo -------------------------------------------------------------

echo Today is $D[1]day $name, no tape copies today!

echo -------------------------------------------------------------

endif

16. 一个 finger 的程式

set FINGER = "/usr/ucb/finger"

if ( -x $FINGER ) then

if ( $#argv == 0 ) then

cat << TAG

---------------------------------

Hahahah ....

---------------------------------

TAG

else

$FINGER "$*"

endif

else

echo "Cannot find finger on this system."

endif

17. 取得变数的方法

set W = `who -r`

echo $W[9]

18. 更改档案名称,将 *.html --> *.htm

# rename *.html to *.htm

echo -n "This will change *.html to *.htm. Can I continue ? (y/n) : "

set input = $<

if ( $input != "y" && $input != "Y" ) then

echo "Ok. "

exit 2

endif

foreach file ( *.html )

echo "Processing $file to `basename $file .html`.htm "

mv $file `basename $file .html`.htm

end

--------------------------------------------------------------------

19. 更改档案名称,将 *.htm --> *.html

echo -n "This will change *.htm to *.html. Can I continue ? (y/n) : "

set input = $<

if ( $input != "y" && $input != "Y" ) then

echo "Ok. "

exit 2

endif

# rename *.htm to *.html

foreach file ( *.htm )

echo "Processing $file to `basename $file .htm`.html "

mv $file `basename $file .htm`.html

end

20. 将大写的档名改成小写的档名

tr string1 string2 会将 standard input 的字串,

所对应到的 string1, 都以 string2 取代

foreach file ( * )

mv $file `echo $file | tr '[A-Z]' '[a-z]'`

end

21. 将小写的档名改成大写的档名

foreach file (*)

mv $file `echo $file | tr '[a-z]' '[A-Z]'`

end

发布者:admin,转转请注明出处:http://www.yc00.com/web/1688986092a191792.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信