lng@ubuntu:~/myshell$ myData=hello
lng@ubuntu:~/myshell$ echo $myData
hello
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ myData="hello word"
lng@ubuntu:~/myshell$ echo $myData
hello word
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ myData="welcome to xian"
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ echo "$myData"
welcome to xian
lng@ubuntu:~/myshell$ echo '$myData'
$myData
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ echo $HOME
/home/lng
lng@ubuntu:~/myshell$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
echo $0
echo $#
echo $$
lng@ubuntu:~/myshell$ ./myTest.sh
./myTest.sh
0
4053
lng@ubuntu:~/myshell$ ./myTest.sh aa bb cc
./myTest.sh
3
4054
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
echo $1
echo $2
echo $3
echo $@
echo $*
lng@ubuntu:~/myshell$ ./myTest.sh aaa 111 fff
aaa
111
fff
aaa 111 fff
aaa 111 fff
lng@ubuntu:~/myshell$
| 字符串比较 | 结果 |
|---|---|
| string1 = string2 | 如果两个字符串相同则结果为真 |
| string1 != string2 | 如果两个字符串不同则结果为真 |
| -n string | 如果字符串不为空则结果为真 |
| -z string | 如果字符串为null则结果为真 |
| 算术比较 | 结果 |
|---|---|
| expression1 -eq expression2 | 如果两个表达式相等则结果为真 |
| expression1 -ne expression2 | 如果两个表达式不等则结果为真 |
| expression1 -gt expression2 | 如果expression1大于expression2则结果为真 |
| expression1 -ge expression2 | 如果expression1大于等于expression2则结果为真 |
| expression1 -lt expression2 | 如果expression1小于等于expression2则结果为真 |
| expression1 -le expression2 | 如果expression1小于等于expression2则结果为真 |
| ! expression | 如果表达式为假则结果为真 |
| 文件条件测试 | 结果 |
|---|---|
| -d file | 如果文件是一个目录则结果为真 |
| -f file | 如果文件是一个普通文件则结果为真 |
| -r file | 如果文件可读则结果为真 |
| -w file | 如果文件可写则结果为真 |
| -x file | 如果文件可执行则结果为真 |
| -s file | 如果文件大小不为0则结果为真 |
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
str="hello"
if [ $str = "hello" ];then
echo "str = hello"
fi
lng@ubuntu:~/myshell$ ./myTest.sh
str = hello
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
num1=100
num2=200
if [ $num1 -eq $num2 ]
then
echo "num1 eq num2"
else
echo "num1 no eq num2"
fi
lng@ubuntu:~/myshell$ ./myTest.sh
num1 no eq num2
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ ls
a.txt b.txt dire myTest.sh
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
if test -f a.txt;then
echo "a.txt is file"
fi
if [ -f b.txt ];then
echo "b.txt is file"
fi
if [ -d dire ];then
echo "dire is directory"
fi
lng@ubuntu:~/myshell$ ./myTest.sh
a.txt is file
b.txt is file
dire is directory
lng@ubuntu:~/myshell$
if condition
then
statements
else
statements
fi
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
echo "please input yes or no!"
#读取终端输入
read temp
if [ $temp = "yes" ]
then
echo "you input is yes"
else
echo "you input is no"
fi
lng@ubuntu:~/myshell$ ./myTest.sh
please input yes or no!
yes
you input is yes
lng@ubuntu:~/myshell$
if condition1
then
statements
elif condition2
then
statements
else
statements
fi
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
echo "please input yes or no!"
#读取终端输入
read temp
if [ $temp = "yes" ]
then
echo "you input is yes"
elif [ $temp = "no" ]
then
echo "you input is no"
else
echo "you input unknow"
fi
lng@ubuntu:~/myshell$ ./myTest.sh
please input yes or no!
fff
you input unknow
lng@ubuntu:~/myshell$
for variable in values
do
statements
done
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
for fruit in apple bnana pear;
do
echo "I like $fruit"
done
lng@ubuntu:~/myshell$ ./myTest.sh
I like apple
I like bnana
I like pear
lng@ubuntu:~/myshell$
while condition; do
statements
done
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
echo "please input password!"
read pwd
while [ "$pwd" != "admin" ];do
echo "Sorry, try again"
read pwd
done
exit 0
lng@ubuntu:~/myshell$ ./myTest.sh
please input password!
123456
Sorry, try again
000
Sorry, try again
admin
lng@ubuntu:~/myshell$
case variable in
pattern [ | pattern ] ...) statements;;
pattern [ | pattern ] ...) statements;;
...
esac
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
echo "please input num"
read buf
case "$buf" in
one) echo "input is one";;
two) echo "input is two";;
three) echo "input is three";;
four) echo "inpuit is four";;
esac
lng@ubuntu:~/myshell$ ./myTest.sh
please input num
three
input is three
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
echo "please input yes or no"
read buf
case "$buf" in
yes|y|YES|Y) echo "input is yes";;
n*|N*) echo "input is no";;
esac
lng@ubuntu:~/myshell$ ./myTest.sh
please input yes or no
y
input is yes
lng@ubuntu:~/myshell$ ./myTest.sh
please input yes or no
noo
input is no
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
myFunc(){
echo "this is myFunc"
}
echo "------"
myFunc
echo "++++++"
lng@ubuntu:~/myshell$ ./myTest.sh
------
this is myFunc
++++++
lng@ubuntu:~/myshell$
lng@ubuntu:~/myshell$ cat myTest.sh
#! /bin/sh
myFunc(){
echo "param num is $#"
echo "param 1 is $1, param 2 is $2"
echo "this is myFunc"
}
echo "------"
myFunc aaa bbb
echo "++++++"
lng@ubuntu:~/myshell$ ./myTest.sh
------
param num is 2
param 1 is aaa, param 2 is bbb
this is myFunc
++++++
lng@ubuntu:~/myshell$
| 退出码 | 说明 |
|---|---|
| 126 | 文件不可执行 |
| 127 | 命令未找到 |
| 128及以上 | 出现一个信号 |
lng@ubuntu:~/myshell$ myData="hello"
lng@ubuntu:~/myshell$ echo $myData
hello
lng@ubuntu:~/myshell$ unset myData
lng@ubuntu:~/myshell$ echo $myData
lng@ubuntu:~/myshell$
lng@ubuntu:~/share/test$ ls
evp_aes.cpp evp_aes.h evp_aes.o main.cpp main.o makefile res
lng@ubuntu:~/share/test$ find ~ -name main.o
/home/lng/share/getopt/main.o
/home/lng/share/test/main.o
lng@ubuntu:~/share/test$
lng@ubuntu:~/share/test$ ls -la
total 92
drwxr-xr-x 2 lng lng 4096 Aug 28 2021 .
drwxrwxr-x 15 lng lng 4096 Apr 30 05:18 ..
-rwxr--r-- 1 lng lng 3760 Jul 31 2021 evp_aes.cpp
-rwxr--r-- 1 lng lng 2115 Jul 31 2021 evp_aes.h
-rw-rw-r-- 1 lng lng 22312 Aug 28 2021 evp_aes.o
-rwxr--r-- 1 lng lng 57 Aug 28 2021 main.cpp
-rw-rw-r-- 1 lng lng 11904 Aug 28 2021 main.o
-rwxr--r-- 1 lng lng 203 Aug 28 2021 makefile
-rwxrwxr-x 1 lng lng 32488 Aug 28 2021 res
# 查找大于10k文件
lng@ubuntu:~/share/test$ find ./ -size +10k
./res
./main.o
./evp_aes.o
lng@ubuntu:~/share/test$
lng@ubuntu:~/share/test$ ls
evp_aes.cpp evp_aes.h evp_aes.o main.cpp main.o makefile res
# 查找普通文件
lng@ubuntu:~/share/test$ find ./ -type f
./main.cpp
./evp_aes.h
./evp_aes.cpp
./res
./main.o
./makefile
./evp_aes.o
lng@ubuntu:~/share/test$