您的位置:首页 > 运维架构 > Shell

shell脚本的编写

2009-12-27 00:57 281 查看




[root @ localhost root]# vi example.c //建立(example.c)并打开文件
//编辑文本
set one two three //set命令设置参数,one two three
one=1 //给参数赋值
two=2
three=3
echo "$1 $2 $3" //显示参数的内容
按ESC键 :wq(保存退出)
[root @ localhost root]# touch file.c //建立文件(file.c)
[root @ localhost root]# vi file.c //打开文件编辑(file.c)
//输入一下内容

#svi3
#save and invoke vi
# //加#注释语句
DIR=$HOME/keep //DIR=路径,#HOME路径默认是/root
if [ $# = 1 ] //如果程序中命令行参数的个数等于1
then
cp $1 $DIR //复制参数文件到/root/keep下
vi $1 //编辑参数文件
else
echo "you must specify a file name.Try again." //否则,显示“你必须是正确的文件名,在试一次”
fi
exit 0 //退出shell脚本

按ESC键,:wq(保持退出)

[root @ localhost root]# chmod a+x file.c //给文件执行的权限
[root @ localhost root]# ./file.c example.c //运行./file.c 文件,后面加example.c文件参数
[root @ localhost root]#touch file1.c //创建文件(touch file1.c)
[root @ localhost root]#vi file1.c //打开编辑文件(vi file1.c)
//编辑文件
#!/bin/bash
#set parameters in the if then
# //#号后的为注释内容
if [ "$1" = "hello" ] //如果文件名为hello,输出hello!how are you?
then
echo "hello! how are you ?"
elif [ "$1" = "" ] //如果文件名为空,输出you must input parameters
then
echo "you must input parameters"
else
echo "The only accept parameter is hello" //否则,输出The only accept parameter is hello
fi
按ESC键:wq(保存退出)
[root @ localhost root]#chmod a+x file1.c //给文件(file1.c)赋予执行权限
[root @ localhost root]#./file1.c hello //执行file1.c hello
hello! how are you ?
[root @ localhost root]#./file1.c //执行file1.c
you must input parameters
[root @ localhost root]#./file1.c file.c //执行file1.c +(除hello或空格的文件名)
The only accept paramenter is hello

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: