Linux Shell 脚本攻略阅读笔记第1章 小试牛刀

一、简介

1.Bash(Bourne Again Shell),目前大多数GNU/Linux系统默认的shell环境。

命令都是在shell终端中输入并执行。打开终端后,提示符的形式:[email protected]$       或    [email protected] #     ($表示普通用户,#表示管理员用户root)

2.shell脚本是一个以#!(shebang)起始的文本文件,如下:   #!/bin/bash

shebang是一个文本行,其中#!位于解释器路径之前。/bin/bash是Bash的解释器命令路径。

3.两种运行脚本的方式。

一种是将脚本作为bash到命令行参数

[email protected]:~/lssc/chpt1$ bash test_script.sh
hello,world

另一种是授予脚本执行权限,将其变为可执行文件。chmod a+x script.sh

[email protected]:~/lssc/chpt1$ chmod a+x test_script.sh
[email protected]:~/lssc/chpt1$ ./test_script.sh
hello,world

内核会读取脚本的首行,注意到shebang为#! /bin/bash。它识别出/bin/bash,并在内部像这样执行该脚本:$  /bin/bash test_script.sh

其中,test_script.sh文件内容如下:

#!/bin/bash
echo hello,world

4.~表示主目录,通常是/home/user,其中user是用户名;若是root用户,则为/root。

5.登录shell是登录主机后获得的那个shell。如果是登录图形界面环境如GNOME、KDE后打开了一个shell,就不是登录shell。

6.Bash中,每个命令或命令序列是以分号或换行符来分隔。#指明注释的开始,延续到行尾。如 $  cmd1  ;  cmd2  等同于 $  cmd1  $  cmd2

二、终端打印

终端是交互式工具,可以通过它与shell环境进行交互。

1.echo.默认在每次调用后添加一个换行符。-n 忽略结尾换行符。

2.echo的三种形式打印:直接打印、单引号、双引号

buxiz[email protected]:~/lssc/chpt1$ echo "Welcome to Bash"
Welcome to Bash
[email protected]:~/lssc/chpt1$ echo Welcome to Bash
Welcome to Bash
[email protected]:~/lssc/chpt1$ echo 'Welcome to Bash'
Welcome to Bash

三种方法的副作用:

直接打印:不能显示文本中的分号——分号在Bash中是命令定界符。echo hello;hello被认为是两个命令echo hello和hello。

单引号:变量替换在单引号中无效。

双引号:不能打印叹号!。或将其转义,\!。确切地说,叹号在末尾、或者叹号后没有空格,则不能正常打印。如下面测试,可以看到,叹号后面有空格时还是可以打印的。

[email protected]:~/lssc/chpt1$ echo "Hello world !"
bash: !": event not found
[email protected]:~/lssc/chpt1$ echo "cannot include exclamation - ! within"
cannot include exclamation - ! within
[email protected]:~/lssc/chpt1$ echo "c!c"
bash: !c": event not found
[email protected]:~/lssc/chpt1$ echo "c ! c"
c ! c
[email protected]:~/lssc/chpt1$ echo "c! c"
c! c
[email protected]:~/lssc/chpt1$ echo "c !c"
bash: !c": event not found

3.printf.无自动换行符。参数类似于C语言,参数以空格分隔。如下:

#!/bin/bash
#文件名:printf.sh

printf "%-5s %-10s %-4s\n" No Name   Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
printf "%-5s %-10s %-4.2f\n" 3 Jeff 77.564

运行显示:

[email protected]:~/lssc/chpt1$ bash printf.sh
No    Name       Mark
1     Sarath     80.35
2     James      91.00
3     Jeff       77.56
时间: 2024-08-04 21:13:59

Linux Shell 脚本攻略阅读笔记第1章 小试牛刀的相关文章

《Linux Shell脚本攻略》 笔记 第三章:文件操作

<Linux Shell脚本攻略> 笔记 第三章:文件操作 1.生产任意大小的文件 [[email protected] dd_test]# [[email protected] dd_test]# dd if=/dev/zero of=junk.data bs=1k count=10 10+0 records in 10+0 records out 10240 bytes (10 kB) copied, 0.00137023 s, 7.5 MB/s 2.文件系统相关测试 [ -f $file

《Linux Shell脚本攻略》 笔记 第四章:高效文本处理

<Linux Shell脚本攻略> 笔记 第四章:高效文本处理 1.IP地址的正则表达式: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} 2.grep用法 //在多级目录中对文本进行递归检索 [[email protected] program_test]# grep "yang" ./ -Rn ./test.txt:6:laoyang ./right.txt:1:1 yang man //忽略大小写匹配 [[email pr

《Linux Shell脚本攻略》 笔记 第六章:打包压缩

<Linux Shell脚本攻略> 笔记 第六章:打包压缩 //1.打包.解包 [[email protected] program_test]# tar -cf output.tar 11.txt 22.txt 33.txt [[email protected] program_test]# tar -xf output.tar -C ./tar-file/  //-C指定要提取到哪个路径? //列举出归档文件中的内容 [[email protected] program_test]# ta

《Linux Shell脚本攻略》 笔记 第一章:Shell起步基础

<Linux Shell脚本攻略> 笔记 第一章:Shell起步基础 1.变量:在bash中,每一个变量的值都是字符串.无论你给变量赋值时,有没有使用引号,值都会以字符串的形式存储. 2.var=value; //赋值操作 var = value: //相等操作 3.获取字符串的长度 [[email protected] ~]$ var=yang [[email protected] ~]$ length=${#var} [[email protected] ~]$ echo $length

《Linux Shell脚本攻略》 笔记 第二章:常用命令

<Linux Shell脚本攻略> 笔记 第二章:常用命令 1.cat cat -s //多个空白行压缩成一个 cat *.txt | tr -s '\n'   //移除空白行 cat -n //加行号 2.find 沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作. eg: find ./ ! -name "*.txt" -print [[email protected] program_test]# find ./  -type f -name "

LINUX SHELL脚本攻略笔记[速查]

Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述符和重定向 cat 数组和关联数组 alias date 调试脚本 函数和参数 管道 读取命令输出 read 字段分隔符和迭代器 循环 比较和测试 find xargs tr md5sum sha1sum 对目录进行校验 sort uniq tempfile split bash变量匹配切分 exp

Linux Shell脚本攻略(1.10)

1.10 获取.设置日期和延时 很多应用程序需要以不同的格式打印日期.设置日期和时间.根据日期和时间执行某项操作.延时通常用于在程序执行过程中提供一段等待时间(比如1秒).同样的,我们也能够一多种格式打印日期,或者在命令行中设置日期.在类Unix系统中,日期被存储为一个整数,其大小为自世界标准时间起所流逝的秒数.这种计时方式称为纪元时或Unix时间. 1.10.1 获取.设置时间 以下的程序给出了多种用法: #!/bin/bash start=$(date +%s) #获取纪元时间 date #

Linux Shell脚本攻略(1.8)

1.8 使用别名 linux中的别名就相当于windows中的快捷方式,使用别名可以省去用户输入一长串命令序列的麻烦. 1.8.1 创建临时别名(快捷方式) alias new_command='command sequence' #格式说明 alias install='sudo apt-get install' #实例说明 在声明 install='sudo apt-get install'之后,就可以用install代替'sudo apt-get install'了.使用这种方式声明的别名

Linux Shell脚本攻略(1.2)

1.2 终端打印 终端是交互式工具,用户可以通过它与shell环境进行交互.在终端中打印文本是大多数shell脚本和工具日常需要执行的基本任务.通过终端打印,人们可以知道系统的运行状态,这对用户来说是至关重要的. echo终端打印 echo "Welcome to Bash" echo 'Welcome to Bash' echo Welcome to Bash 以上三种方法的效果是一样的,输出内容都是"Welcome to Bash",并在末尾添加换行符.在默认情