打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux shell 语法详解

linux shell是强大的脚本程序,语法简单,下面是一个可执行的sh脚本,涵盖了常用的shell语法和用法,理解它,就等于入门了。

 

#!/bin/bash
# title :
# date  : ?2012/10/18
# author: Made by hfx
echo '[test @test test]# sh [-nvx] scripts
-n :不要执行 scripts ,查询 scripts 内的语法,若有错误则予以列出!
-v :在执行 scripts 之前,先将 scripts 的内容显示在屏幕上;
-x :将有使用到的 scripts 内容显示在屏幕上,与 -v 稍微不同 '


echo '转义字符\后面带符号'
hello=Hello\ \!\ How\ are\ you\ \?
echo $hello

echo '单引号和双引号的区别:单引号里的$只表示$,双引号里的$表示变量'
name="V.Bird"
myname1="My name is $name"
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2

echo '显示声明变量declare -i'
declare -i a=3
declare -i b=4
#declare -i c=$a * $b
#echo $c

echo '交互'
echo 'cin you name:'
read name
echo 'your name is:'
echo $name

echo 'shell 程序的参数'
echo '$0 表示sh程序后的第0个参数,$1……'

echo '条件语句'
echo 'continue please cin:y or Y'
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
 echo "script is running..."
else
 echo "STOP!"
fi

echo 'case语句'
echo "Press your select one, two, three!"
read choice
case $choice in                              
 one)
  echo "your choice is one"
  ;;
 two)
  echo "your choice is two"
  ;;
 three)
  echo "your choice is three"
  ;;
 *)
  echo "Usage {one|two|three}"  
  #exit 1
esac

echo '循环语句for()'
echo '1+……+100'
declare -i s  # <==变量宣告
for (( i=1; i<=100; i=i+1 ))
do
 s=s+i
done

echo "1+... + 10?0 = $s"

echo 'do...while'
echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
 read yn
done
echo "Stop here"

echo 'test shell end!'

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux Shell编程入门
shell 十三問之第十二问:你要 if 還是 case
第五章 Shell 编程
linux shell中的点命令
求linux 下source、sh、bash、./执行脚本的区别?
[ps1]Linux下PS1、PS2、PS3、PS4使用详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服