#! /bin/bash 复制代码
echo "Hello World!"
复制代码
echo "What is your name"
将输入的值复制给变量 PERSON.
read PERSON
# $PERSON 取出变量的值
echo "Hello $PERSON "
复制代码
定义变量不能使用 shell 的关键字,可以使用 help 命令查看变量的等号 =左右不能有空格.使用变量在前面加上美圆符号 $ 或 ${}, 良好的编程方式是加上 {}, 放置变量的界限不清楚c++
url=http://www.baidu.com
echo ${url}
name='百度一下'
echo ${name}
author="Kevin"
echo ${author}
复制代码
url=http://www.taobao.com
echo ${url}
复制代码
单引号的会按照原来的方式输出,哪怕其中有变量也不会处理,双引号会解析变量和命令web
url=http://www.huawei.com
website1='欢迎来到: ${url}'
website2="欢迎来到: ${url}"
echo ${website1}
echo ${website2}
复制代码
variable=`cat log.txt`
variable=$(cat log.txt)
echo ${variable}
复制代码
经过使用 readonly 将变量定义为只读变量,这个值就不能再修改shell
myUrl="http://www.baidu.com"
readonly myUrl
# 提示错误: 变量不能修改
# myUrl="http://www.huawei.com"
复制代码
unset 只能删除非 readonly 的变量编程
mUrl="http://www.huawei.com"
unset mUrl
echo ${mUrl}
复制代码
$ 符号表示当前进程的 ID, 即 pid数组
$0 当前脚本的文件名bash
$n 传递给脚本或函数的参数,n 是一个数字,表示第几个参数,第一个是 $1, 第二个是$2less
$# 传递给脚本或函数的参数个数函数
$@ 传递给脚本或函数的全部参数, 被双引号包含时与 $* 稍有不一样工具
$* 传递给脚本或函数的全部参数测试
$? 上个命令的退出状态,或函数的返回值
$$当前进程的 pid
echo $$
# 当前脚本的文件名
echo $0
复制代码
echo "First Name: $0"
echo "First Parameter: $1"
echo "Second Parameter: $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters: $#"
复制代码
echo "\$*=" $*
echo "\"\$*\"=" "$*"
echo "\$@=" $@
echo "\"\$@\"=" "$@"
echo "print each param from \$*"
for var in $*
do
echo "${var}"
done
echo "print each param from \$@"
for var in $@
do
echo "$var"
done
echo "print each param from \"\$*\""
for var in "$*"
do
echo "$var"
done
echo "print each param from \"\$@\""
for var in "$@"
do
echo "$var"
done
复制代码
DATE=`date`
echo "Date is ${DATE}"
USERS=`who | wc -l`
echo "Logged in user are ${USERS}"
UP=`date; uptime`
echo "Uptime is ${UP}"
复制代码
${var:-""} 若是变量 var 为空将不会赋值
echo ${var13:-"Wariable is not set"}
echo "1 - value of var is ${var13}"
# ${var:=""} 若是变量为空,将会被复制成后面的值
echo ${var13:="Variable is not set"}
echo "2 - value of var is ${var13}"
# unset var13,删除 var13
unset var13
# ${var:} 若是变量为空,则会返回后面的值,但不会赋值给 var
echo ${var13:+"This is default vale"}
echo "3 - value of var is ${var13}"
# ${var:+""} 若是 var 有值,返回 word, 但不改变 var 的值
var13="Prefix"
echo ${var13:+"This is default value"}
echo "4 - value of var is ${var13}"
# ${var:?""} 若是 var 为空或被删除,会将后面的值做为错误输出,并会中止脚本运行
echo ${var13:?"Print this message"}
echo "5 - value of var is ${var13}"
# var14 未定义,将 This is a error message 输出
# echo ${var14:?"This is a error message"}
# echo "6 - value of var is ${var14}"
复制代码
expr 是一个表达式计算工具 2 + 2 运算符之间必须有空格
var14=$(expr 2 + 2)
echo "Total value: ${var14}"
a14=10
b14=20
# 加法
var14=`expr $a14 + $b14`
echo "a + b : $var14"
var14=`expr $a14 - $b14`
echo "a - b : $var14"
var14=`expr $a14 \* $b14`
echo "a * b : $var14"
var14=`expr $a14 / $b14`
echo "a / b : $var14"
var14=`expr $a14 % $b14`
echo "a % b : $var14"
if [ $a14 == $b14 ]
then
echo "a is euqal to b"
fi
if [ $a14 != $b14 ]
then
echo "a is not equal b"
fi
复制代码
a15=10
b15=20
# 检查两个值是否相等
if [ $a15 -eq $b15 ]
then
echo "$a15 -eq $b15 : a is equal to b"
else
echo "$a15 -eq $b15 : a is not equal to b"
fi
# 检查两个值是否不相等,不想等返回 true
if [ $a15 -ne $b15 ]
then
echo "$a15 -ne $b15: a is not equal to b"
else
echo "$a15 -ne $b15: a is equal to b"
fi
# 检查左边的值是否大于右边的,若是是,返回 true
if [ $a15 -gt $b15 ]
then
echo "$a15 -gt $b15 : a is greater than b"
else
echo "$a15 -gt $b15 : a is not greater than b"
fi
# 检查左边的值是否小于右边的,若是是返回 true
if [ $a15 -lt $b15 ]
then
echo "$a15 -lt $b15 : a is less than b"
else
echo "$a15 -lt $b15 : a is not less than b"
fi
# 检查左边的值是否大于等于右边的值, 若是是返回true
if [ $a15 -ge $b15 ]
then
echo "$a15 -ge $b15: a is greater or equal to b"
else
echo "$a15 -ge $b15: a is not greater or equal to b"
fi
# 检查左边的值是否小于等于右边的值, 若是是返回 true
if [ $a15 -le $b15 ]
then
echo "$a15 -le $b15: a is less or euqal to b"
else
echo "$a15 -le $b15: a is not less or euqal to b"
fi
复制代码
a16=10
b16=20
# 判断两个值不相等,若是不相等返回 true
if [ $a16 != $b16 ]
then
echo "$a16 != $b16 : a is not equal to b"
else
echo "$a16 != $b16 : a is equal to b"
fi
# a16 是否小于 100 且 b16 大于 15
if [ $a16 -lt 100 -a $b16 -gt 15 ]
then
echo "$a16 -lt 100 -a $b16 -gt 15 true"
else
echo "$a16 -lt 100 -a $b16 -gt 15 false"
fi
# a16 是否小于 100 或 b16 大于 100
if [ $a16 -lt 100 -o $b16 -gt 100 ]
then
echo "$a16 -lt 100 -o $b16 -gt 100 true"
else
echo "$a16 -lt 100 -o $b16 -gt 100 false"
fi
# a16 小于 5 或 b16 大于 100
if [ $a16 -lt 5 -o $b16 -gt 100 ]
then
echo "$a16 -lt 5 -o $b16 -100 15 true"
else
echo "$a16 -lt 5 -o $b16 -100 15 false"
fi
复制代码
a17="abc"
b17="efg"
if [ $a17 = $b17 ]
then
echo "$a17 = $b17 : a is equal to b"
else
echo "$a17 = $b17 : a is not equal to b"
fi
if [ $a17 != $b17 ]
then
echo "$a17 = $b17 : a is not equal to b"
else
echo "$a17 = $b17 : a is equal to b"
fi
if [ -z $a17 ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a17 ]
then
echo "-n $a17: string length is not zero."
else
echo "-n $a17: string length is zero."
fi
if [ $a17 ]
then
echo "$a17 : string is not empty"
else
echo "$a17 : string is empty"
fi
复制代码
file="./log.txt"
# 检查文件是否可读
if [ -r $file ]
then
echo "File has read access."
else
echo "File does not have read access."
fi
# 检查文件是否为空.
if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi
# 检查文件是否存在
if [ -e $file ]
then
echo "File exist"
else
echo "File does not exist"
fi
# 检查是不是目录.
if [ -d $file ]
then
echo "File is a directory."
else
echo "This is not a directory"
fi
# 检查文件是否普通文件.
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
# 检查文件是否可执行.
if [ -x $file ]
then
echo "File has execute permission."
else
echo "File does not have execute permission."
fi
# 检查文件是否可写
if [ -w $file ]
then
echo "File has write permission."
else
echo "File does not have write permission."
fi
复制代码
# 获取字符串长度
string="abcd"
echo ${#string}
# 截取字符串
string="hello world"
echo ${#string:1:5}
# 查找字符串
#string="I love you."
#echo `expr index "$string" is`
# 拼接字符串
string="hello"
string1="world"
string2="shell"
string3="$string $string1, $string2"
echo $string3
复制代码
# 定义数组
array=(1 2 3 4 5 6 "zhangsan")
# 获取指定元素 ${array_name[index]}
valuen=${array[6]}
echo "valuen: $valuen"
# 获取数组中全部元素, ${array_name[*]} 或 ${array_name[@]}
echo "array: ${array[@]}"
# 获取数组的长度, 跟字符串获取长度方式相同:${#array_name[@]} 或 ${#array_name[*]}
length=${#array[*]}
echo "array length: ${length}"
# 获取数组中单个元素的长度
lengthn=${#array[6]}
echo "The element length of array: $lengthn"
复制代码
#重定向到文件
file="./log.txt"
echo "Hello Echo." > $file
复制代码
a21=20
b21=30
if [ $a21 -gt $b21 ]
then
echo "a21 gt b21"
else
echo "a21 lt b21"
fi
if [ $a21 -eq 100 ]
then
echo "a21 -eq 100"
elif [ $a21 -eq 50 ]
then
echo "a21 -eq 50"
elif [ $a21 -le 20 ]
then
echo "a21 -eq 20"
else
echo "a21 -lt 20"
fi
复制代码
数值测试.
num1=10
num2=20
if test ${num1} -eq ${num2}
then
echo "The two numbers are equal!"
else
echo "The two numbers are not equal"
fi
# 字符串测试
num1="abc"
num2=abc
if test num1=num2
then
echo "The two strings are equal!"
else
echo "The two strings are not equal!"
fi
# 文件测试
if test -e ./log.txt -o ./bash
then
echo "One file already exists!"
else
echo "The file does not exists!"
fi
if test -x ./learningShell.sh
then
echo "The file can execute."
else
echo "Them file does not execute."
fi
复制代码
echo "Input a number between 1 to 4"
echo "Your number is: \c"
read aNum
case $aNum in
1) echo 'you select 1'
;;
2) echo 'you select 2'
;;
3) echo 'you select 3'
;;
*) echo 'You do not select a number between 1 to 4'
esac
# option="${1}"
# case ${option} in
# -f) FILE="${2}"
# echo "File name is $FILE"
# ;;
# -d) DIR="${2}"
# echo "Dir name is $DIR"
# ;;
# *)
# echo "`basename ${0}`: usage: [-f file] | [-d directory]"
# exit 1
# ;;
# esac
复制代码
for i in 1 2 3 4 5
do
if [ $i -eq 0 ]
then
echo "The value is 0."
elif [ $i -eq 1 ]
then
echo "The value is 1."
else
echo "The value is others: $i"
fi
done
# 输出字符串.
for str in 'This is a string'
do
echo $str
done
# 输出指定目录下的文件.
for FILE in $HOME/fullstack/c++/LearningCpp/*
do
echo $FILE
done
复制代码
# 遍历输出某个条件下的数据.
COUTER=0
while [ $COUTER -lt 5 ]
do
COUTER=`expr $COUTER + 1`
echo $COUTER
done
# 读取键盘的数据.
# echo 'type <CTRL-D> to terminate'
# echo -n 'enter your most liked film: '
# while read FILM
# do
# echo "Yeah! greate film the $FILM"
# done
复制代码
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=$(expr $a + 1)
done
复制代码
# 符合条件后跳出.
while :
do
echo -n "Input a number between 1 to 5:"
read aNum
case $aNum in
1|2|3|4|5) echo "Your number is ${aNum}!"
;;
*)
echo "You do not select a number between 1 to 5, game is over!"
break
;;
esac
done
# 跳到指定层
for var in 1 2 3
do
for var2 in 0 5
do
if [ $var -eq 2 -a $var2 -eq 0 ]
then
break 2
else
echo "$var $var2"
fi
done
done
# continue
while :
do
echo -n "Input a number between 1 to 5: "
read aNum
case $aNum in
1|2|3|4|5) echo "Your number is ${aNumb}!"
;;
0) echo "Completed!"
break
;;
*)
echo "You do not select a number between 1 to 5!"
continue
echo "Game is over!"
;;
esac
done
复制代码
# 定义一个函数
Hello () {
echo "This is a function."
}
# 调用函数
Hello
# 有返回参数的函数
funcWithReturn() {
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNumber
echo "The two numbers are ${aNum} and ${anotherNumber} !"
return $(expr $aNum + $anotherNumber)
}
funcWithReturn
ret=$?
echo "The sum of two numbers is $ret !"
# 函数嵌套
one() {
echo "function one."
two
}
two() {
echo "function two."
}
one
# 删除函数
# unset .f one
复制代码
给函数传递参数
$# 参数个数 $* 显示全部传递给函数的参数 $@ 显示全部参数,在有引号的是不一样 $? 函数的返回值
funcWithReturn() {
echo "first parameter is $1 "
echo "second parameter is $2"
echo "third parameter is $3"
echo "tenth parameter is ${10}"
}
funcWithReturn 1 2 3 4 5 6 7 8 9 10
复制代码
引入外部的脚本,包含的脚本能够没有执行权限 . xx.sh 或 source xx.sh
#. ./test.sh
source ./test.sh
echo $name
复制代码