shell脚本 编写

来源:互联网 发布:昆明java 编辑:程序博客网 时间:2024/06/07 00:01

简单学习了几个demo,记录一下

1.如何在shell脚本中使用变量

#!/bin/bashset -eecho "this is a test"echo "there are all files"COUNT=5echo $COUNTif test $COUNT>3then echo "变量大于3"echo $JAVA_HOMEfi

2 如何在shell脚本中创建调用函数

#!/bin/bashfunction testFunction (){   echo "this is a function test"}testFunction

3 如何在shell脚本中读取变量

#!/bin/bashset -eecho "please input a num:"read numNUM={$num}0000歇会儿0000000echo "this is a test"echo $NUM

4如何在shell脚本中使用循环语句

#!/bin/bash# 出现错误就停止执行当前脚本set -eint=1while(($int<=5))doecho $intecho "this is a test"if test $int = 3then exitfilet "int++"done

(其中let “int++”)是给变量赋值
5如何执行一个shell脚本
(1)bin/bash xxx.sh 直接执行该shell脚本
(2)chmod -x xxx.sh 赋予该shell脚本可执行权限