Shell Grammer

语言分类:编译语言,解释语言,汇编语言

编译语言: C / C++ / JAVA
强类型: 使用前必须声明变量类型
过程:源程序-->编译 -->链接 -->执行
基本变量类型:字符、整数、浮点数

解释语言: BASH / PERL / PYTHON / PHP / ruby
弱类型:直接使用变量
过程:源程序-->执行
默认变量类型:字符

"":弱引用 变量替换
‘’:强引用 不做变量替换
`:命令替换

声明变量:declare
语法:declare <类型> <变量名>
类型:-i 整数 、-r 只读变量、-x 环境变量、 -a 数组

释放变量: unset
语法:unset <变量名>

只读变量(常量):readonly
语法:readonly <变量名>

默认环境变量都是大写,自定义变量尽量小写或者首字母大写

config file sequence
Login shell:
1. /etc/profile
2. ~/.bash_profile
3. ~/.bashrc
4. /etc/bashrc
Last run file is ~/.bash_profile

Normal shell:
1. ~/.bashrc
2. /etc/bashrc

PS1 提示符
\W: relative path
\w: absolute path
\h: hostname
\u: username
\t: 24 hour
\T: 12 hour
Default PS1="[\u@\h \W] \\$"
Exampe: PS1="\u \h \w \t \\$"

系统变量:
/etc/profile, ~/.bash_profile:  会话时读取一次
/etc/bashrc, ~/bashrc:  每次打开终端读取
/etc/profile.d:为不同版本shell特殊用法说明
用户变量
.bash_profile:  会话时读取一次
.bashrc:    每次打开终端读取
.bash_history: 记录用户以前输入的命令
.bash_logout:  用户退出是要执行的命令
用户变量
定义:me
赋值:me=jingzhao
显示:echo $me/read me   
删除:unset me
只读:readonly me
转化成环境变量:export me
显示环境变量:env/export/set
判断语句:
第一种方式:[]/test/&&/||
[]:  进行数据判断
判断条件:
1.每个组件需要用空格来分隔;
2.变量用双引号来设置;
3.常量用单引号或者双引号来设置
例子:
[ -z "$HOME" ]
[ "$HOME" == "$MAIL" ]
test: 进行数据判断
例子:test$HOME=$MAIL 
&&:  前面的命令执行结果为正确执行后面的命令;否则就略过;
例子:ls /tmp && touch /tmp/testingagain
||:  前面的命令执行结果为错误执行后面的命令;否则就略过
例子:ls /tmp && touch /tmp/testingagain
&&和||一起用:command1&&command2||command3
例子:ls /tmp/vbirding && echo "exist"||echo "not exist"
复合使用例子:
#!/bin/bash
#Program
#    this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
read -p "Please input (Y/N): "yn
[ "$yn" == "Y" -o "$yn" == "y" ]&& echo "OK, continue" && exit 0
[ "$yn" == "N" -o "$yn" == "n" ]&& echo "Oh, interrupt!" && exit 0
echo "I don't know what is your choise" && exit 0
第二种方式:if...then
结构一:
if[条件判断表达式]; then
  当判断判断表达式,执行语句
fi
例子:
#!/bin/bash
#Program
#    this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
read -p "Please input (Y/N): "yn
if [ "$yn" == "Y" ]||[ "$yn" == "y" ]; then
  echo "OK, continue"
  exit 0
fi
if [ "$yn" == "N" ]||[ "$yn" == "n" ]; then
  echo "Oh, interrupt!"
  exit 0
fi
echo "I don't know what is your choise" && exit 0
结构二:
if[条件判断表达式1]; then
  当判断表达式1成立,执行语句
elif[条件判断表达式2]; then
  当判断表达式2成立,执行语句
else
  当判断表达式1,2均不成立,执行语句
fi
例子:
#!/bin/bash
#Program
#    this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
read -p "Please input (Y/N): "yn
if [ "$yn" == "Y" ]||[ "$yn" == "y" ]; then
  echo "OK, continue"
elif [ "$yn" == "N" ]||[ "$yn" == "n" ]; then
  echo "Oh, interrupt!"
else
  echo "I don't know what is your choise"
fi
第三种方式:case...esac
结构:
case $变量名称 in
   "第一个变量内容")
      符合第一个变量情况下程序段
      ;;
   "第二个变量内容")
      符合第二个变量情况下程序段
      ;;
    *)
      第一,二个变量内容均不符合情况下程序段
      exit 1
      ;;
esac
例子:
#!/bin/bash
#Program
#    this program will show the use's choice
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
echo "This program will show the user's choice!"
#read -p "Input your choice:" choice
#case $choice in
case $1 in
  "one")
    echo "Your choice is ONE"
    ;;
  "two")
    echo "Your choice is TWO"
    ;;
  "three")
    echo "Your choice is THREE"
    ;;
  *)
    echo "Usage (one|two|three)"
    ;;
esac
循环语句:
第一种方式:while do done
结构:
while [ condition ]
do
   程序段落
done
说明:当conditions满足执行;否则终止
例子:
#!/bin/bash
#Program
#    Use loop to try find your input
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
while [ "$yn" != "yes" ]&&[ "$yn" != "YES" ]
do
   read -p "Please input yes/YES to stop this program: " yn
done
第二种方式:until do done
结构:
until [ condition ]
do
   程序段落
done
说明:当conditions不满足执行;否则终止
例子:
#!/bin/bash
#Program
#    Use loop to try find your input
#History
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
until [ "$yn" == "yes" ]||[ "$yn" == "YES" ]
do
   read -p "Please input yes/YES to stop this program: " yn
done
第三种方式:for...do....done
结构:
for ((初始值;限制值;执行布长))
do
  程序段
done
说明:适用于数值运算
#!/bin/bash
#Program
#    Try do calculate 1+2+3....+100
#History:
#2010/12/24 Tom first release
PATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/bin
export PATH
s=0
for ((i=1;i<=100;i++))
do
  s=$(($s+$i))
done
echo "The result of '1+2+3...+100' is ==> $s"
1. for循环例子:
#!/bin/bash
for ((i=1;i<10000000;i++))
do
touch $i;
done
作用:在文件夹下产生大量空文件;
目的:演示inode的功能,如果inode被用完,即使有空间,也无法新建文件。
补充:查看inode:df -i
#!/bin/bash
for ((i=1; i < 100000; i++))
do
echo -n 'A' >> log.txt;
sleep 1;
done
作用:每秒在文件log.txt中追加一个'A'
parameter meaning:
$# number of parameter
$1 the first parameter
$@ all the parameters
$0 program
$? the result of the previous command
$$ the pid of the command

运算符描述示例
文件比较运算符
-e filename如果 filename 存在,则为真[ -e /var/log/syslog ]
-d filename如果 filename 为目录,则为真[ -d /tmp/mydir ]
-f filename如果 filename 为常规文件,则为真[ -f /usr/bin/grep ]
-L filename如果 filename 为符号链接,则为真[ -L /usr/bin/grep ]
-r filename如果 filename 可读,则为真[ -r /var/log/syslog ]
-w filename如果 filename 可写,则为真[ -w /var/mytmp.txt ]
-x filename如果 filename 可执行,则为真[ -L /usr/bin/grep ]
filename1 -nt filename2如果 filename1 比 filename2 新,则为真[ /tmp/install/etc/services -nt /etc/services ]
filename1 -ot filename2如果 filename1 比 filename2 旧,则为真[ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)
-z string如果 string 长度为零,则为真[ -z "$myvar" ]
-n string如果 string 长度非零,则为真[ -n "$myvar" ]
string1 = string2如果 string1 与 string2 相同,则为真[ "$myvar" = "one two three" ]
string1 != string2如果 string1 与 string2 不同,则为真[ "$myvar" != "one two three" ]
算术比较运算符
num1 -eq num2等于[ 3 -eq $mynum ]
num1 -ne num2不等于[ 3 -ne $mynum ]
num1 -lt num2小于[ 3 -lt $mynum ]
num1 -le num2小于或等于[ 3 -le $mynum ]
num1 -gt num2大于[ 3 -gt $mynum ]
num1 -ge num2大于或等于[ 3 -ge $mynum ]
 

Comments

Popular posts from this blog

Nginx Proxy & Load Balance & LNMP

Snort+barnyard2+Snorby CentOS 6.5_64 Installation

ORACLE Error