linux入门-shell(.sh)脚本编写和运行
2017-07-29 21:54
801 查看
编写第一个shell脚本
在gedit中编写.sh格式的文件,保存为a.sh。代码: #! /bin/bash # employ bash shell player1=xiaoming # define a player1 player2=ken echo "Game start! $player1 $player2" # echo is used to printf in terminal
在终端调用脚本,定位到目录,然后输入:
bash a.sh
看到打印结果如上所示。
编写第一个if/else脚本
编写的if/else如下:if和 ; 之间的代码
ls -l a.sh
是用来判断当前的目录下是否存在a.sh这个文件。if和else的基本格式如下所示,
if command ; then
code1
else
code2
fi
在终端调用的结果如下,可以看到输出了
ls return true
再看一个if/else脚本
如下脚本,if/else格式和上面格式一致,重点看下 if 和 ;间的那个命令,命令开始以 [,后面有4个参数 $1,=,me,]输出的结果如下所示:
用 = 来判断输入的字符串是否等于me。其他的常用判断参数见下图:
看一个for循环
for循环的格式如下脚本所示:#! /bin/bash # employ bash shell for num in 1 2 3 4 5 six do echo "num=$num" for(( num=1; num<7; num++)) # method2 do echo "num=$num" done
while循环
while循环的基本格式如下:#! /bin/bash i=7 j=10 while [ $i -lt $j ] do echo "num1 = $i, num2=$j" ((i++)) done
相关文章推荐
- 自己编写的.sh脚本文件运行完闪退解决方案
- 运行shell脚本,错误:-bash: ./xxx.sh: /bin/bash^M: bad interpreter: No such file or directory
- windows下编写的shell脚本如何在Linux运行
- windows下编写的Shell脚本在Linux下运行错误的解决方法
- windows下编写的Shell脚本在Linux下运行错误的解决方法
- 解决在windows环境中编写shell脚本,在Linux运行出错的问题
- ubuntu下编写shell脚本运行 启动goagent
- 一段评价程序平均运行时间的shell脚本&shell脚本编写注意事项
- linux 用notepad++编写shell脚本出现/bin/sh^M: bad interpreter: 没有那个文件或目录l的解决方案
- 【Unix/Linux编程实践】shell如何运行程序—编写命令解析器sh
- windows下编写shell脚本运行出错问题
- Linux开机如何自动运行自己编写好的shell脚本
- Linux crontab的使用方式,sh脚本的编写,sh脚本自动启动tomcat服务器,sh监控系统运行情况
- windows下编写的shell脚本在Linux下运行,提示Syntax error
- windows下编写的Shell脚本在Linux下运行错误的解决方法
- 朋友的一个问题:Linux开机如何自动运行自己编写好的shell脚本
- windows下编写shell脚本放到linux运行问题
- cygwin安装教程以及使用notepad++编写shell脚本,并通过cygwin运行
- 自己编写的shell脚本开机运行
- Linux crontab的使用方式,sh脚本的编写,sh脚本自动启动tomcat服务器,sh监控系统运行情况