shell programming tutorial

可以直接 man bash 学习语法和相关命令。


一、什么是shell程序

以文件形式存放批量的Linux命令集合,该文件能够被Shell解释执行,这种文件就是Shell脚本程序

通常由一段Linux命令、Shell命令、控制语句以及注释语句构成

Shell 脚本的编写

Shell 脚本是纯文本文件,可以使用任何文本编辑器编写

Shell 脚本通常是以 .sh 作为后缀名

第一行:指定用哪个程序来编译和执行脚本。#!/bin/bash(shell 变量里面含 ! x 的话,要转义一下 \! x)

注释行:使用(#)符号;多行注释  <<EOD ... EOD


二、shell编程的主要内容

变量

本地变量、环境变量、位置参量

输入输出

read/echo或printf

条件测试

整数测试、逻辑测试、字符串测试

控制语句

条件/循环/分支/

函数
常用Shell程序内置指令

declare/export/eval/trap等


三、变量

(一)变量概述




(二)变量常见操作



(三)环境变量和只读变量



(四)位置参量(命令行参数)

 位置参量是一组特殊的内置变量,通常被 shell 脚本用来从命令行接受参数,或被函数用来保存传递给它的参数。

 执行 shell 脚本时,用户可以通过命令行向脚本传递信息,跟在脚本名后面的用空格隔开的每个字符串都称为位置参量。

在脚本中使用这些参数时,需通过位置参量来引用。例如: $1 表示第一个参数,$2 表示第二个参数,以此类推。 $9 以后需要用花

括号把数字括起来,如第 10 个位置参量以  ${10} 的方式来访问。


$* 和 $@ 都表示传递给函数或脚本的所有参数,不被双引号(" ")包含时,都以"$1" "$2" … "$n" 的形式输出所有参数。但是当它们被双引号(" ")包含

时,"$*" 会将所有的参数作为一个整体,以"$1 $2 … $n"的形式输出所有参数;"$@" 会将各个参数分开,以"$1" "$2" … "$n" 的形式输出所有参数。

注意 “$*” 受 $IFS 变量值的影响,如 

$ IFS=:;  
$ set x y z  
$ echo $*  
x y z  
$ echo "$*"  
x:y:z  
$ echo $@  
x y z  
$ echo "$@"  
x y z  

使用$?判断管道的执行结果是否成功,不可信。因此在使用管道获取的参数,我们一定要增加对于参数的判断,或者,我们在sh和bash的解释器中,

增加set –o pipefail 的设置,可以让管道的执行结果是否成功,变得可信。


#例1:shell_test.sh 
echo “the count of parameters:$#”
echo “first param=$1”
echo “second param=$2”
echo “params’ string=$*”

shell_test.sh This is Peter

shell_test.sh This is "Peter Piper"    //如果位置参量中含有空格,则需要使用双引号


(五)数组



# 字典必须先声明

declare -A dic

dic=([key1]="value1" [key2]="value2" [key3]="value3")

#打印指定key的value

echo ${dic["key1"]}

#打印所有key值

echo ${!dic[*]}

#打印所有value

echo ${dic[*]}


四、输入输出

(一)输入--read命令

read var

从标准输入读取一行并赋值给变量var

read

标准输入读取一行并赋值给内置变量REPLY

read -a arr

读入一组词,依次赋值给数组arr


read -p "please input 5 digits:" -t 10 -a arr3  // -p 提示符 -t 超时

read -s // 输入不回显

stty -echo // 输入不回显    

stty echo // 输入回显


(二)输出--echo 命令

echo $num 或 echo ${num}   //输出一行文本

echo -n “Hello World”  // -n 去掉换行符
echo -e “\t” “Hello World”   // -e 支持转义


(三)echo输出颜色与光标定位:

\33[30m -- \33[37m   设置前景色(字体颜色)
\33[40m -- \33[47m   设置背景色
\33[y;xH设置光标位置


echo –e “\033[31mthis is a test”
echo -e "\033[10;5H\033[31m\033[46m this is a test“
echo -e "\033[0m" // 取消全部设置


五、算术扩展

(一)单引号、双引号、反引号区别

单引号 忽略所有特殊字符
双引号 忽略大部分特殊字符($,`, \ 等字符除外) 参考这里 或者尝试  X=*;   echo $X;   echo '$X';  echo "$X"; 的区别
反引号 命令替换(将一个命令的标准输出插入到命令的任何位置)
$()     同上
命令替换可以嵌套
 如果使用反引号,则内部的反引号必须用反斜杠来转义。

echo  `basename \`pwd\``

echo  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learning Linux Shell Scripting 2nd Edition pdf Break through the practice of writing tedious code with shell scripts Key Features Learn to impeccably build shell scripts and develop advanced applications Create smart solutions by writing and debugging scripts A step-by-step tutorial to automate routine tasks by developing scripts Book Description Linux is the most powerful and universally adopted OS. Shell is a program that gives the user direct interaction with the operating system. Scripts are collections of commands that are stored in a file. The shell reads this file and acts on commands as if they were typed on the keyboard. Learning Linux Shell Scripting covers Bash, GNU Bourne Again Shell, preparing you to work in the exciting world of Linux shell scripting. CentOS is a popular rpm-based stable and secured Linux distribution. Therefore, we have used CentOS distribution instead of Ubuntu distribution. Linux Shell Scripting is independent of Linux distributions, but we have covered both types of distros. We start with an introduction to the Shell environment and basic commands used. Next, we explore process management in Linux OS, real-world essentials such as debugging and perform Shell arithmetic fluently. You'll then take a step ahead and learn new and advanced topics in Shell scripting, such as decision making, starting up a system, and customizing a Linux environment. You will also learn about grep, stream editor, and AWK, which are very powerful text filters and editors. Finally, you'll get to grips with taking backup, using other language scripts in Shell Scripts as well as automating database administration tasks for MySQL and Oracle. By the end of this book, you will be able to confidently use your own shell scripts in the real world. What you will learn Familiarize yourself with the various text filtering tools available in Linux Understand expressions and variables and how to use them practically Automate decision-making and save a lot of time

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值