C shell学习笔记(一)综合
1 #!/bin/csh
2 可用set与env命令查看环境变量与当前值
3 @、set xxx=xxx 设置局部数值变量/变量;setenv 设置全局变量
eg. set name 赋空值
set name = (John Doe)
setenv name “John Doe”
4 命令替换
eg. set command = `pwd`
echo “The … is : $command”
显示:The … is : /…/…
5 所有UNIX命令在执行成功时传回一个退出状态0,在失败时传回非0。一个命令传回的状态值被保存在只读环境变量$?中,可以由调用进程检查。
6 从标准输入读
set xxx=$<
或者set xxx=`head –l`
eg. #!/bin/csh
echo –n “Enter input:”
set line=`head –l`
echo “You entered: $line”
exit 0
7 unset xxx
unsetenv xxx
8 向脚本传递参数 eg:
#!/bin/csh
echo “The command name is : $0”
echo “The number of command line arguments are $#argv”
echo –n “The value of the command line arguments are:”
echo “$argv[1] $argv[2] …”
echo “Another way to display value of all of the arguments : $argv[*]”
exit 0
9-1 if (…) then
…
else if (…) then
…
else if (…) then
…
else
…
endif
9-2 foreach variable (argument list)
command list
end
9-3 while (expression)
…(真时)
end(假时)
9-4 switch (“$string”)
case “yangfeng”:
…
breaksw
case “panye”:
…
breaksw
default:
…
breaksw
endsw
10 数值变量
eg. @ a=10
@ b=15
@ difference = ( $a - $b ) sum = ( $a + $b )
@ a++
@ a+=1
@ area = $a*$b
11 数组 set xxx = (…………………………)
$#xxx元素个数 $?xxx是否初始化(返回1表示已初始化)
$xxx $xxx[i]
用set命令对任何shell变量赋以多个值,将会使其成为一个数组
12 连接多个字符串eg.set thisfile=”$directory”/”$file[$index]”
评论