1. 首页
  2. IT资讯

Shell 语法

系统变量:/etc/profile:会话时读取一次/etc/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 releasePATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/binexport 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 0echo “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 releasePATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/binexport PATH

read -p “Please input (Y/N): “ynif [ “$yn” == “Y” ]||[ “$yn” == “y” ]; thenecho “OK, continue”exit 0fiif [ “$yn” == “N” ]||[ “$yn” == “n” ]; thenecho “Oh, interrupt!” exit 0fiecho “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 releasePATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/binexport PATH

read -p “Please input (Y/N): “ynif [ “$yn” == “Y” ]||[ “$yn” == “y” ]; thenecho “OK, continue”elif [ “$yn” == “N” ]||[ “$yn” == “n” ]; thenecho “Oh, interrupt!” elseecho “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 releasePATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/binexport PATH

echo “This program will show the user’s choice!”#read -p “Input your choice:” choice#case $choice incase $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 releasePATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/binexport PATHwhile [ “$yn” != “yes” ]&&[ “$yn” != “YES” ] doread -p “Please input yes/YES to stop this program: ” yndone

第二种方式:until do done结构:until [ condition ]do程序段落done说明:当conditions不满足执行;否则终止例子:#!/bin/bash#Program#Use loop to try find your input#History:#2010/12/24 Tom first releasePATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/binexport PATHuntil [ “$yn” == “yes” ]||[ “$yn” == “YES” ] doread -p “Please input yes/YES to stop this program: ” yndone

第三种方式:for…do….done结构:for ((初始值;限制值;执行布长))do程序段done说明:适用于数值运算#!/bin/bash#Program#Try do calculate 1+2+3….+100#History:#2010/12/24 Tom first releasePATH=/bin;/sbin;/usr/bin;/usr/sbin;/usr/local/bin;/usr/local/sbin;~/binexport PATHs=0for ((i=1;i<=100;i++))do s=$(($s+$i))doneecho “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’

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29578568/viewspace-2136953/,如需转载,请注明出处,否则将追究法律责任。

主题测试文章,只做测试使用。发布者:布吉卡,转转请注明出处:http://www.cxybcw.com/195334.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code