格式要求:首行shebang機制
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
檢測腳本中的語法錯誤
bash -n /path/to/some_script
調試執行
bash -x /path/to/some_script
局部變量 ? ??變量賦值:name=‘value’
環境變量 ? ??變量聲明、賦值:
export name=VALUE
declare -x name=VALUE
定義函數
f_name (){
…函數體…
}
函數變量作用域:
環境變量:當前shell和子shell有效
本地變量:只在當前shell進程有效,為執行腳本會啟動專用子shell進程;
因此,本地變量的作用范圍是當前shell腳本程序文件,包括腳本中的函數
局部變量:函數的生命周期;函數結束時變量被自動銷毀
根據輸入的數字判斷年齡多大
#!/bin/bash
read -p “please input your age: ” age
[[ “$age” =~ ^[0-9]+$ ]] || { echo your age is false ; exit 10; }
if [ “$age” -gt 0 -a “$age” -le 18 ];then
echo you are very young
elif [ “$age” -gt 18 -a “$age” -le 50 ];then
echo “work hard”
elif [ “$age” -gt 50 -a “$age” -le 80 ];then
echo “you are retire and enjoy you life”
elif [ “$age” -gt 80 -a “$age” -le 150 ] ;then
echo “Very OK”
else
echo “you don not come from the earth”
fi
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/92699