bash极简入门教程
note:
(1)如果提示执行脚本没有权限-bash: ./xxx.sh: Permission denied则通过chmod添加执行权限chmod 777 demo1.sh。
(2)文件中的第一行#!/bin/bash用来确定文件类型,不能省略。She Bang#!字符序列和解释器路径之间、还有#之前都不能有任何空格。
(3)通过#单行注释,通过<BLOCK首尾连接多行注释。
(4)打印or读取(通过read读取)变量时,需要在变量名前加上$,这个地方也是比较特殊的,在下面2.3-2.5中也会看到。
文章目录
- note:
- 一、bash极简介绍
- 二、 基础操作
- 2.1 创建和运行bash脚本
- 2.2 清除/var/log的log文件内容
- 2.3 进制转换
- 2.4 使用分号(一行写多个命令)
- 2.5 双分号终止case选项
- 2.6 单引号比双引号更加强烈(阻止解释)
- 2.7 简单循环语句
- 2.8 定义函数
- Reference
一、bash极简介绍

- bash,解释性语言,脚本语言,胶水语言(可以通过将系统调用、公共程序、工具和编译过的二进制程序”粘合“在一起来建立应用) 。对于重复工作和管理系统任务上挺方便。
- bash就是linux系统中自带的shell(命令行窗口)
二、 基础操作
2.1 创建和运行bash脚本
(1)touch demo1.sh创建脚本
(2)vim demo1.sh进入脚本编辑内容(也可以使用gedit等其他编辑器),编辑如下内容,其中echo是输出打印的意思。
#!/bin/bash
# this is the first bash scriptecho "hello andy"
(3)退出文件编辑,运行脚本:./demo1.sh或者source demo1.sh,即可显示hello andy,这是一个最简单的demo啦。
note:
(1)如果提示执行脚本没有权限-bash: ./xxx.sh: Permission denied则通过chmod添加执行权限chmod 777 demo1.sh。
(2)文件中的第一行#!/bin/bash用来确定文件类型,不能省略。She Bang#!字符序列和解释器路径之间、还有#之前都不能有任何空格。
(3)通过#单行注释,通过<BLOCK首尾连接多行注释。
(4)打印or读取(通过read读取)变量时,需要在变量名前加上$,这个地方也是比较特殊的,在下面2.3-2.5中也会看到。
2.2 清除/var/log的log文件内容
该文件记录了我们平时apt安装的软件包的信息,如果要写一个脚本把文件内容清空,但是保留文件。/dev/null可以看做一个黑洞(无内容),所以这样重定向能够清空文件。
#!/bin/bash# 初始化一个变量
LOG_DIR=/var/logcd $LOG_DIRcat /dev/null > dpkg.logecho "Logs cleaned up."exit
2.3 进制转换
将二进制101011转为十进制。
#!/bin/bash
echo "The # here does not begin a comment."
echo 'The # here does not begin a comment.'
echo The \# here does not begin a comment.
echo The # 这里开始一个注释
echo $(( 2#101011 )) # 数制转换(使用二进制表示),不是一个注释,双括号表示对于数字的处理
在屏幕输出:
The # here does not begin a comment.
The # here does not begin a comment.
The # here does not begin a comment.
The
43
2.4 使用分号(一行写多个命令)
判断当前目录是否有ttt.sh脚本文件,没有就创建之。这里的if else语句需要以fi结尾。
#!/bin/bash
echo hello; echo there
filename=ttt.sh
if [ -e "$filename" ]; then # 注意: "if"和"then"需要分隔,-e用于判断文件是否存在echo "File $filename exists."; cp $filename $filename.bak
elseecho "File $filename not found."; touch $filename
fi; echo "File test complete."
2.5 双分号终止case选项
当然是输出abc:
#!/bin/bash
varname=b
case "$varname" in[a-z]) echo "abc";;[0-9]) echo "123";;
esac
2.6 单引号比双引号更加强烈(阻止解释)
可以看到单引号的没有解释:
#!/bin/bash
HOME='hello'
echo HOME # hello
echo "$HOME" # hello
echo '$HOME' # $HOME
打印出:
HOME
hello
$HOME
2.7 简单循环语句
语法和很多语言类似:
for loop in 1 2 3 4 5
doecho "The value is: $loop"
donefor str in This is a string
doecho $str
done
打印:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
This
is
a
string
2.8 定义函数
(1)无传参函数:定义两个读入的数,进行相加的简单函数,函数返回值在调用该函数后通过$?来获得。
#!/bin/bash
funWithReturn(){echo "This function will add the two numbers of the input..."echo "Enter the first number: "read aNumecho "Enter the second number: "read anotherNumecho "The two numbers are $aNum and $anotherNum !"return $(($aNum+$anotherNum))
}
funWithReturn
echo "The sum of the two numbers entered is $? !"
(2)有传参函数:
#!/bin/bash
funWithParam(){echo "The first parameter is $1 !"echo "The second parameter is $2 !"echo "The tenth parameter is $10 !"echo "The tenth parameter is ${10} !"echo "The eleventh parameter is ${11} !"echo "The total number of parameters is $# !"echo "Outputs all parameters as a string $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
Reference
[1] https://www.yiibai.com/bash/bash-relative-vs-absolute-path.html
[2] Bash编程入门-1:Shell与Bash
[3] 三分钟学会用Bash写脚本
[4] shell基础教程:http://c.biancheng.net/shell/base/
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
