shell是一个用C语言编写的程序,是用户使用linux的桥梁。一种和内核沟通的外壳应用程序的统称。
shell在各个岗位完成的任务:
shell&bash的关系比喻:shell是媒婆,bash就是王婆。
shell脚本:是一种为shell编写的脚本程序。
特点:解释非编译型;弱类型
执行模式:交互式/批处理式
echo命令用于向窗口输出文本
#表示注释,如果#位于第一行开头,并且是则例外,它表示该脚本使用后面指定的解释器/bin/bash解释器。
crontab: 在某一固定时间执行某一特定任务
查看计划任务:
[root@localhost a]# crontab -l
no crontab for root
pwd
cd ..
pwd
[a@localhost ~]$ sh shell.sh
/home/a
/home
[a@localhost ~]$ pwd
/home/a
[a@localhost ~]$ cd ..
[a@localhost home]$ pwd
/home
[a@localhost home]$
脚本执行cd命令,发现回显消息当前所处的目录发生改变,但实际上真实目录并没有改变。(创建子进程解释脚本)
直接在命令行上执行cd命令,发现父bash的工作目录发生改变。(不需要创建子进程的命令,叫做shell的内置命令,由父bash亲自执行)
[a@localhost ~]$ sh shell.sh
hello
[a@localhost ~]$ vim shell.sh
[a@localhost ~]$ sh shell.sh //创建子进程执行
/home/a
/home
[a@localhost ~]$ pwd
/home/a
[a@localhost ~]$ source shell.sh //父进程自己执行
/home/a
/home
[a@localhost home]$ pwd
/home
用source修饰脚本,脚本的执行影响到父bash!source或者.命令是shell的内建命令,不创建子shell,而是直接在交互式shell下逐行执行脚本中的命令。
变量名命名规则:
等号两边不能有空格,可使用下划线。
首个字符必须为字母。
不能使用标点符号。
不能使用bash里的关键字。
mystring="hello world"
echo "$mystringhello bit"
[a@localhost ~]$ sh shell.sh
bit
shell将mystring和hello认为了一个新的变量名,而该变量名并没有被使用过,故为空串。
加上花括号就可以正常输出了:(花括号是为了帮助编译器识别变量的边界,推荐使用)
mystring="hello world"
echo "${mystring}hello bit"
[a@localhost ~]$ sh shell.sh
hello worldhello bit
只读变量:使用readonly命令可以将变量定义为只读变量。
readonly mystring="hello world"
echo $mystring
mystring="hello bit"
echo $mystring
删除变量:使用unset,但unset命令不能使用只读变量
set命令可以显示当前shell进程中定义的所有变量和函数。
printenv命令可以显示当前进程的环境变量
export命令可以把本地变量导出为环境变量
字符串拼接:
mystr="hehe"
mystr2="hehe2"
echo $mystr$mystr2
[a@localhost ~]$ sh shell.sh
hehehehe2
获取字符串长度:
str='hello'
echo ${#str}
[a@localhost ~]$ sh shell.sh
5
提取子字符串:
string="abcdefg"
echo ${string:1:4}
[a@localhost ~]$ sh shell.sh
bcde
查找子字符串:(返回子字符串的下标)
string="bit is a great company"
echo `expr index '$string' is`
[a@localhost ~]$ sh shell.sh
2
通配符:
*:匹配0个或多个任意字符
?:匹配一个任意字符
[a@localhost ~]$ for i in {1..100};do touch file$i; done
[a@localhost ~]$ ls file*
file1 file18 file27 file36 file45 file54 file63 file72 file81 file90
file10 file19 file28 file37 file46 file55 file64 file73 file82 file91
file100 file2 file29 file38 file47 file56 file65 file74 file83 file92
file11 file20 file3 file39 file48 file57 file66 file75 file84 file93
file12 file21 file30 file4 file49 file58 file67 file76 file85 file94
file13 file22 file31 file40 file5 file59 file68 file77 file86 file95
file14 file23 file32 file41 file50 file6 file69 file78 file87 file96
file15 file24 file33 file42 file51 file60 file7 file79 file88 file97
file16 file25 file34 file43 file52 file61 file70 file8 file89 file98
file17 file26 file35 file44 file53 file62 file71 file80 file9 file99
[a@localhost ~]$ ls file[13579]
file1 file3 file5 file7 file9
[a@localhost ~]$ ls file2*
file2 file21 file23 file25 file27 file29
file20 file22 file24 file26 file28
[a@localhost ~]$ ls file5?
file50 file51 file52 file53 file54 file55 file56 file57 file58 file59
[a@localhost ~]$
命令代换:
#echo `expr index '$string' is`
date=`date +%Y:%m:%d`
[a@localhost ~]$ sh shell.sh
2018:06:04
算数代换:
myval=1+1
echo $myval
((myval=1+1))
echo $myval
[a@localhost ~]$ sh shell.sh
1+1
2
如果要创建一个文件名以-号开头的文件是不行的:
[a@localhost ~]$ touch -file
touch: invalid option -- 'i'
Try `touch --help' for more information.
如果非要处理以-号开头的文件名:
[a@localhost ~]$ touch -- -file
shell脚本中的单引号和双引号一样都是字符串的界定符,而不是字符的界定符。
单引号用于保持引号内所有字符的字面值,即使引号内的\和回车也不例外。
echo "hello bit $mystring \\ \" \\ \` `date +%Y:%m:%d`"
echo 'hello bit $mystring \\ \" \\ \` `date +%Y:%m:%d`'
[a@localhost ~]$ sh shell.sh
hello bit \ " \ ` 2018:06:04
hello bit $mystring \\ \" \\ \` `date +%Y:%m:%d`
双引号用于保持引号内所有字符的字面值以下情况例外:
加变量名可以取变量的值反引号仍表示命令替换
加
变
量
名
可
以
取
变
量
的
值
反
引
号
仍
表
示
命
令
替
换
表示$的字面值
表示
的字面值(反引号)
“表示”的字面值
\ 表示\的字面值