bash base
系统命令自带的程序,一般在/bin或/usr/bin目录下。
一般使用shell编程:
- 备份文件
- 安装软件
- 下载数据
- 对文件操作等。
shell主要用来管理文件和运行程序。
shell和shell脚本的概念
shell是指一种应用程序。
shell脚本(shell script)一种为shell编写的脚本程序。
通常所说的shell是指shell script。
运行方式
shbash- 给执行权限之后,运行改文件。
chmod +x filename
shell 脚本是一种弱类型语言。
shell 是用来解决用户如何与操作系统通信的问题。
shell 就是壳,区别于核。
#!/usr/bin/env bash
#!/bin/bash#!是一个约定的标记,它告知系统这个脚本需要什么解释器来执行。
变量
#!/user/bin/env bash
# set
NAME="john"
# get
echo "hello $NAME!"变量名和等号之间不能有空格
字符串
NAME="john"
NAME2='masa'
NAME3=`asha`双引号与``可以解析变量,单引号直接输出。
拼接字符串:
NAME="john"
greeting="hello, "$NAME" !"
greeting_1="hello, ${NAME} !"
echo $greeting $greeting_1获取字符串长度:
str='abcdefg'
echo ${#str}提取子字符串:
str="company"
echo ${str:1:4}流程控制
if:
#!/usr/bin/env bash
if [ -z "$str" ]; then
echo "String is empty"
elif [ -n "$str" ]; then
echo "String is not empty"
fifor:
for i in {1..5}; do
echo "Welcome $i"
done
for i in {5..50..5}; do
echo "Welcome $i"
done
cat 1.sh | while read line; do
echo $line
done
sum=0
count=1
for i in {1..100}; do
sum=$[$sum+$count]
count=$[$count+1]
done
echo $sumswitch:
echo "$1"
case "$1" in
start)
echo start;;
stop)
echo stop;;
*)
echo hello;;
esac函数
# 定义函数
myfunc () {
echo "hello $1"
}
# 调用函数
echo "You are $(get_name)"
# 定义函数 参数
function myfunc () {
echo "hello world - $*"
}
# 调用函数
myfunc "John"
# 定义函数 局部变量
myfunc4 () {
local myresult="some value"
echo $myresult
}
# 调用
myfunc
# 定义
myfunc3 () {
return 1
}
# 调用
if myfunc3; then
echo 'success'
else
echo "failure"
fi输入
# $0为程序的路径和名称,$1...$9 ,${10}参数
# $# 参数的个数
echo `basename $0`
if [ $1 != 2 ]; then
echo '2'
firead -p "Enter one or more values >"
echo "REPLY = '$REPLY'"set 命令
#!/usr/bin/env bash
set -eux
set -o pipefail
echo 123