文档介绍:Linux bash shell 第七组:齐小川、张文利郭成、赵伟利杨红梅、韩秀秀张闻棋、翟江潮 Linux Bash Shell 编程-Shell 简介? Shell 在 UNIX/Linux 操作系统中,既是一种命令解释器, 又是一种程序设计语言。作为人机交互的命令解释器, Shell 将用户键入的命令字符行转换成内核能够理解的形式, 继而执行相应的功能。作为程序设计语言,它支持绝大多数在高级语言中能见到的程序元素,如函数、变量、数组和程序控制结构等。? UNIX/Linux 中常见的 shell 有 ash 、 Bourne Shell (sh) 、 bash (Bourne Again Shell) 、 ksh 、 pdksh 、 csh 、 tcsh 、 pdksh 、 zsh 等。 Linux Bash Shell 编程-Shell 语法? Shell 变量 Shell 变量命名规则①变量名必须以字母或下划线开始,后面跟字母、数字或下划线组成的字符序列②不要使用 Shell 语法关键字(如 export 、 while 、 for 等)或系统保留特殊字符(如‘?’、‘*’等) ③变量名区分大小写 Shell 变量定义与赋值?变量的定义和赋值是同步进行的,变量值均当成字符串, 未定义的变量值为空串?变量定义和赋值的基本格式 varname = 字符串 Shell 变量的引用变量引用基本格式: $varname 或${varname} 注:当引用未经定义或初始化的变量,其引用结果为空值赋值与引用举例: str1=hello,world str1= ” hello,world str1= ’ hello,world ’ export a=50 CurDir=`pwd` CurDir=$(pwd) echo $str1 PATH= “$PATH:$HOME/bin ” Shell 中预定义的特殊变量#:存储 Shell 程序中命令行参数的个数?:存储上一执行命令的返回值 0-9 :存放脚本程序命令的位置参数*:存储 Shell 程序的所有参数@:存储所有命令行输入的参数? Shell 条件语句 If语句基本格式 if expressionl then commands elif expression2 then commands else commands fi #!/bin/bash #This is example about if statement. echo – n "Please input the answer: " read I if [ $I = y ] Then echo "The answer is right" elif [ $I = n ] then echo "The answer is wrong" else echo "Bad Input." fi #end 该脚本文件实现读取一个字符,然后根据字符的值显示不同的内容 case 语句基本格式 case variable in pattern1 commands;; pattern2 commands;; … patternn commands;; esac #!/bin/bash echo "----------" echo "1 Restore “ echo "2 Backup" echo "3 Upload" echo -n "Enter Choice: " read CHOICE case "$CHOICE" in 1|R) echo "1 Restore";; 2|B) echo "2 Backup";; 3|U) echo "3 Upload" ;; * ) echo "sorry $CHOICE is not a valid choice! " exit 1 esac #end 使用 case 语句建立一个文本行的选择菜单,并根据输入显示选择的选项? Shell 循环语句 for 循环语句基本格式?格式 1: for var in list do commands done ?格式 2 for var do commands done