基础Shell脚本大法
shell中的变量变量的设置规则读取变量的时候,$PATH 与 $是等同的双引号内的特殊符号如$等,可以保持原有的特性。单引号内的特殊符号则仅为一般字符(纯文本)`命令` 与 $(命令)是等同的变量如何加1语法((i=i+1));let i=i+1;x=$(( $x + 1 ))x=`expr $x
shell中的变量
变量的设置规则
- 读取变量的时候,$PATH 与 ${PATH}是等同的
- 双引号内的特殊符号如$等,可以保持原有的特性。
- 单引号内的特殊符号则仅为一般字符(纯文本)
- `命令` 与 $(命令)是等同的
变量如何加1
语法
((i=i+1));
let i=i+1;
x=$(( $x + 1 ))
x=`expr $x + 1`
实例
until example
#bin/bash
i=1
s=0
until [[ i -gt 30 ]];
do
((s=s+i));
((i=i+1));
done
- 易混淆的变量与字符串
shell中的控制语句
if语句
格式:
if [[ condition ]]; then
#statements
elif [[ condition ]]; then
#statements
else
#statements
fi
if语句内判断参数
| 运算符 | 描述 |
|---|---|
| –b | 当file存在并且是块文件时返回真 |
| -c | 当file存在并且是字符文件时返回真 |
| -d | 当pathname存在并且是一个目录时返回真 |
| -e | 当pathname指定的文件或目录存在时返回真 |
| -f | 当file存在并且是正规文件时返回真 |
| -g | 当由pathname指定的文件或目录存在并且设置了SGID位时返回为真 |
| -h | 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效 |
| -k | 当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真 |
| -p | 当file存在并且是命令管道时返回为真 |
| -r | 当由pathname指定的文件或目录存在并且可读时返回为真 |
| -s | 当file存在文件大小大于0时返回真 |
| -u | 当由pathname指定的文件或目录存在并且设置了SUID位时返回真 |
| -w | 当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。 |
| -o | 当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。 |
文件比较运算符、字符串比较运算符、算术比较运算符*
| 运算符 | 描述 | 示例 |
|---|---|---|
| -e file | 如果 file存在,则为真 | [ -e /var/log/syslog ] |
| -d file | 如果 file 为目录,则为真 | [ -d /tmp/mydir ] |
| -r file | 如果 file可读,则为真 | [ -r /var/log/syslog ] |
| -w file | 如果 file可写,则为真 | [ -w /var/mytmp.txt ] |
| -x file | 如果 file可执行,则为真 | [ -L /usr/bin/grep ] |
| file1 -nt file2 | 如果 file1 比 file2 新,则为真 | [ /tmp/install/etc/services -nt /etc/services ] |
| file1 -ot file2 | 如果 file1 比 file2 旧,则为真 | [ /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 ] |
switch语句
格式:
case word in
pattern )
;;
esac
for语句
for循环的几种写法
#!/bin/bash
for((i=1;i<=10;i++));
do
echo $(expr $i \* 3 + 1);
done
for i in $(seq 1 10)
do
echo $(expr $i \* 3 + 1);
done
for i in {1..10}
do
echo $(expr $i \* 3 + 1);
done
awk 'BEGIN{for(i=1; i<=10; i++) print i}'
for i in `ls`;
do
echo $i is file name\! ;
done
for i in $* ;
do
echo $i is input chart\! ;
done
for i in f1 f2 f3 ;
do
echo $i is appoint ;
done
list="rootfs usr data data2"
for i in $list;
do
echo $i is appoint ;
done
for file in /proc/*;
do
echo $file is file path \! ;
done
for file in $(ls *.sh)
do
echo $file is file path \! ;
done
shell中的函数
$1 是第一个参数。 $2 是第二个参数。 $n 是第n个参数。 $@ 被扩展成$1 $2 $3等。 $* 被扩展成$1c$2c$3c,其中c是IFS的第一个字符。 $# 函数所接受的参数的个数
echo详解
echo不换行刷新数据
#!/bin/bash
while [ 1 ]
do
a=$(ifconfig eth0 | grep 'RX pac' | awk '{print $2}' | awk -F: '{print $NF}')
echo -ne "$a\r" #不换行刷新数据
done
echo
echo颜色输出
格式: echo -e "\e[字背景颜色;字体颜色m字符串\e[0m"
0 重新设置属性到缺省设置 1 设置粗体 2 设置一半亮度(模拟彩色显示器的颜色) 4 设置下划线(模拟彩色显示器的颜色) 5 设置闪烁 7 设置反向图象 2J 清除屏幕 0q 关闭所有的键盘指示灯 1q 设置"滚动锁定"指示灯(Scroll Lock) 2q 设置"数值锁定"指示灯(Num Lock) 3q 设置"大写锁定"指示灯(Caps Lock) 15:40H 把关闭移动到第15行,40列 \007 发蜂鸣生beep
| 字体颜色 | 背景颜色 | |
|---|---|---|
| 黑 | 30 | 40 |
| 红 | 31 | 41 |
| 绿 | 32 | 42 |
| 黄 | 33 | 43 |
| 蓝 | 34 | 44 |
| 紫 | 35 | 45 |
| 深绿 | 36 | 46 |
| 白 | 37 | 47 |
#!/bin/bash
#定义颜色的变量
RED_COLOR='\E[1;31m' #红
GREEN_COLOR='\E[1;32m' #绿
YELOW_COLOR='\E[1;33m' #黄
BLUE_COLOR='\E[1;34m' #蓝
PINK='\E[1;35m' #粉红
RES='\E[0m'
#需要使用echo -e
echo -e "${RED_COLOR}======red color======${RES}"
echo -e "${YELOW_COLOR}======yelow color======${RES}"
echo -e "${BLUE_COLOR}======green color======${RES}"
echo -e "${GREEN_COLOR}======green color======${RES}"
echo -e "${PINK}======pink color======${RES}"
echo "#############################################################"
#直接把echo -e放到变量里面,使用的时候直接输出变量即可
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
echo ----oldboy trainning----- && $SETCOLOR_SUCCESS
echo ----oldboy trainning----- && $SETCOLOR_FAILURE
echo ----oldboy trainning----- && $SETCOLOR_WARNING
echo ----oldboy trainning----- && $SETCOLOR_NORMAL