Shell 编程基础

Shell 编程基础

Hello World

[root@localhost shell]# cat HelloWorld.sh
#!/bin/bash
#This Line is a comment
echo "Hello World"

一个 shell 脚本永远都是 #! 开头,这是一个脚本开始的标记,它是在告诉系统执行这个文件需要使用某个解释器,后面的 /bin/bash 指明了解释器的位置

第二行同样是 # 开头的,但这是一个注释,脚本中以 # 开头的都是注释 (#! 开头除外)

第三行就是输出 Hello World

[root@localhost shell]# bash HelloWorld.sh
Hello World
[root@localhost shell]# ./HelloWorld.sh
-bash: ./HelloWorld.sh: 权限不够
[root@localhost shell]# chmod +x HelloWorld.sh
[root@localhost shell]# ./HelloWorld.sh
Hello World

脚本排错

写代码都很容易出错

比如说语法错误

[root@localhost shell]# cat HelloWorld.sh
#!/bin/bash
#This Line is a comment
ehco "Hello World"
[root@localhost shell]# ./HelloWorld.sh
./HelloWorld.sh:行3: ehco: 未找到命令

如果出现逻辑错误

可以使用 bash -x 观察脚本运行情况

[root@localhost shell]# bash -x HelloWorld.sh
+ echo 'Hello World'
Hello World

内建命令

如何确定内置命令:type

[root@localhost shell]# type cd
cd 是 shell 内嵌
[root@localhost shell]# type ifconfig
ifconfig 是 /usr/sbin/ifconfig

执行程序:. (点号)

[root@localhost shell]# ./HelloWorld.sh
-bash: ./HelloWorld.sh: 权限不够
[root@localhost shell]# . ./HelloWorld.sh
Hello World

别名:alias

[root@localhost shell]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost shell]# alias myShutdown='shutdown -h now'

删除别名:unalias

[root@localhost shell]# unalias myShutdown

任务前后台切换:bg、fg、jobs

[root@localhost shell]# tar -zcf usr.tgz /usr  # 打包 /usr 目录
tar: 从成员名中删除开头的“/”
tar: 从硬连接目标中删除开头的“/”
^Z  # Ctrl+z 暂停前台任务
[1]+  已停止               tar -zcf usr.tgz /usr
[root@localhost shell]# jobs  # 查看当前任务
[1]+  已停止               tar -zcf usr.tgz /usr
[root@localhost shell]# bg 1  # 把 tar 任务放置后台运行
[1]+ tar -zcf usr.tgz /usr &
[root@localhost shell]# jobs
[1]+  运行中               tar -zcf usr.tgz /usr &
[root@localhost shell]# fg 1  # 把 tar 任务放置前台运行
tar -zcf usr.tgz /usr
[root@localhost shell]# tar -zcf usr.tgz /usr &  # 一开始就放入后台运行
[1] 5378
[root@localhost shell]# jobs
[1]+  运行中               tar -zcf usr.tgz /usr &

改变目录:cd

改变当前目录,如果不加参数,默认进入当前用户家目录

声明变量:declare、typeset

# 声明变量 i_num01 并赋值 1
[root@localhost shell]# i_num01=1
# 声明变量 f_num01 并赋值 3.14
[root@localhost shell]# f_num01=3.14
# 声明变量 str01 并赋值 Hello World
[root@localhost shell]# str01="Hello World"
# 声明整型变量 i_num02 并赋值 1
[root@localhost shell]# declare -i i_num02=1
# 声明只读变量 readonly 为 100
[root@localhost shell]# declare -r readonly=100
[root@localhost shell]# readonly=200
-bash: readonly: 只读变量
# 声明一个数组
[root@localhost shell]# declare -a arr='([0]="a" [1]="b" [2]="c")'
[root@localhost shell]# echo ${arr[0]}
a
[root@localhost shell]# echo ${arr[1]}
b
[root@localhost shell]# echo ${arr[2]}
c

打印字符:echo

[root@localhost shell]# echo "Hello World"
Hello World
[root@localhost shell]# echo -n "Hello World"
Hello World[root@localhost shell]# echo "\n"
\n
[root@localhost shell]# echo -e "\n"


跳出循环:break

跳出当前循环,开始下一次循环:continue

将所跟踪的参数作为 Shell 的输入,并执行产生的命令:eval

[root@localhost shell]# cmd="ls -l /etc/passwd"
[root@localhost shell]# eval $cmd
-rw-r--r--. 1 root root 1003 9月  25 12:07 /etc/passwd

[root@localhost shell]# name1=LiuShu
[root@localhost shell]# name2=BaiLiLiuShu
[root@localhost shell]# num=2
[root@localhost shell]# eval echo "$"name$num
BaiLiLiuShu

[root@localhost shell]# name1=ls
[root@localhost shell]# name2=blls
[root@localhost shell]# eval $name1="$name2"
[root@localhost shell]# echo $ls
blls

执行命令来取代当前的 Shell:exec

内置命令 exec 并不启动新的 Shell,而是用要被执行的命令替换当前的 Shell 进程,并且将老进程的环境清洗掉,而且 exec 命令后的其他命令将不再执行

退出 Shell:exit

使变量能被子 Shell 识别:export

发送信号给指定 PID 或进程:kill

整数运算:let

[root@localhost shell]# let i=2+2
[root@localhost shell]# echo $i
4
[root@localhost shell]# let i++
[root@localhost shell]# echo $i
5
[root@localhost shell]# let i+=10
[root@localhost shell]# echo $i
15

显示当前工作目录:pwd

声明局部变量:local

从标准输入读取一行到变量:read

[root@localhost shell]# declare N
[root@localhost shell]# read N
liushu
[root@localhost shell]# echo $N
liushu

定义函数返回值:return

向左移动位置参数:shift

一个脚本运行时可以接受参数,从左到右第一个参数记作 $1,第二个参数记作 $2,第 n 个参数记作 $N,所有参数记作 $@ 或 $*,参数总个数记作 $#,脚本本身记作 $0

显示并设置进程资源限度:ulimit

测试表达式:test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值