shell脚本的编写

[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值