Shell learning simple record

Shell learning simple record


Use vi/vim to create a new file text.sh , sh as the extension does not affect script execution.
#!: Tell the system which interpreter to use
echo: output text to the window

#!/bin/bash
echo "Hello World!"

chomod: make the script have executable permissions./
: execute the script in the current directory

chmod +x test.sh
./test.sh

Run the interpreter directly as the script file name

/bin/sh test.sh

Define variable: your_name="aniu" (There can be no spaces around the equal sign)
for file in $(ls /etc): loop the file name under etc

your_name="aniu"
for file in 'ls /etc'

$your_name: use variable

your_name="aniu"
echo $your_name

readonly: Modified variable variable is read-only variable variable value cannot be modified

your_name="aniu"
readonly your_name

unset: delete variables (variables modified by readonly cannot be deleted)

unset your_name

When running the shell, there will be three kinds of variables at the same time:

  1. Local variables
    are defined in scripts or commands
  2. Environment Variables Environment variables
    accessible to all programs, including programs started by the shell
  3. Shell variables
    Special variables set by the shell program to ensure the normal operation of the shell

shell command line parameters

$0 当前脚本文件名
$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。
$# 传递给脚本或函数的参数个数
$*  传递给脚本或函数的所有参数
$@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到
$? 上个命令的退出状态,或函数的返回值
$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID

#!/bin/bash
#!/bin/bash
echo "File Name: $0"
echo "First Parameter : $1"
echo "First Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

shell string
Single quote: variable cannot exist
Double quote: escape character is allowed when variable exists

str='string'
str="string\"$your_name\""
str="string"$your_name""
str="string${your_name}"
//获取字符串长度
echo ${
    
    #str}//6
//提取字符串
echo ${
    
    str:1:4} //trin
//查找‘r’的位置
echo `expr index "$str" r`//2

Define the array

arr_name=(1 2 3)
//单独定义数组分量
arr_name[0]=1
//读取数组元素
your_name=${
    
    arr_name[0]}
//使用@符号获取数组所有元素
echo ${
    
    arr_name[@]}
//获取数组元素个数
lengh=${
    
    #arr_name[@]}

shell comments

//注释符号
#
//花括号 标记为函数 没有地方调用可以实现 多行注释
{
    
    }

chmod +x test.sh
./test.sh 1 2
Shell passing parameters

//特殊字符处理参数
$#
$*
$$ 脚本运行当前进程号
$! 后台运行的最后一个进程ID号

echo "执行的文件名$0"
echo "第一个参数$1"
echo "第二个参数$2"
echo "参数的个数为$#"
echo "传递参数作为一个字符串显示$*"

Shell basic operation
expr expression calculation tool
2 + 2 There must be a space between the expression and the operator

+
-
* //注意 *前面加\ (反斜杠)
/
%
=
==
!=
sum=`expr 2 + 2`
echo "两数之和:$sum"
mul=`expr 2 \* 2`
echo "两数之积:$mul"
if [ 2 == 2 ]
then 
	echo "2 等于 2"
fi
if [ 2 != 3 ]
then 
	echo "2 不等于 3"
fi

Relational operator

-eq 相等
-ne 不相等
-gt 大于
-lt 小于
-ge 大于等于
-le 小于等于
if [ 2 -le 3 ]
then 
	echo "2 小于等于 3"
fi

Boolean operator

! //非运算
-o //或运算
-a //与运算

logic operation

&&
||

String operator

= //字符串是否相等
!= //两个字符串是否不相等
-z //字符串长度是否为 0
-n //字符串长度是否不为0
str //字符串是否不为空

File test operator

-b file //是否块设备文件
-c file //是否字符设备文件
-d file //是否目录
-f file //是否普通文件
-g file //是否设置了SGID位
-k file //是否设置了粘着位Sticky Bit
-p file //是否有名管道
-u file //是否设置SUID位
-r file // 是否可读
-w file //是否可写
-x file //是否可执行
-s file //是否为空
-e file //是否存在
file="/var/www/aniu/text.sh"
if [ -r file ]
then
	echo "file 可读"
else 
	echo "file 不可读"
fi

read: read from standard input

#! /bin/bash
read name
echo "$name is a test"
//-e 开启转义
echo -e "OK \n"
//-c 不换行
echo -e "OK \c"
//显示结果定向直文件
echo "aniu" >myfile
//显示当前日期
echo `date`
//[]中执行基本运算 不能有空格
result=[2*5]
if con1
then
	com1
elif con2
 	com2
else
	comN
fi

//for循环
for var in item item2
do
	com1
done
//while循环
while con1
do
	com1
done
	例子case......
#!/bin/sh
int=1
while(( $int<=5 ))
do
	echo $int
	//bash let 命令
	let "int++"
done
	例子case.....
#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
	echo $a
	a=`expr $a + 1`
done
//case
echo "输入1到5之间的数字"
echo "输入的数字为:"
read aNum
case $aNum in
	1) echo "选择1"
	;;
	2) echo "选择2"
	;;
	*) echo "没有输入"
	;;
esac
//跳出循环
#!/bin/bash
while :
do
	echo -n "输入1-5"
	read aNum
	case $aNum in
		1|2|3) echo "输入:$aNum"
		;;
		*) echo "结束"
		break
		;;
	esac
done

shell function

#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com

funWithReturn(){
    
    
    echo "这个函数会对输入的两个数字进行相加运算..."
    echo "输入第一个数字: "
    read aNum
    echo "输入第二个数字: "
    read anotherNum
    echo "两个数字分别为 $aNum 和 $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
//$? 获取返回值
echo "输入的两个数字之和为 $? !"
-----------------------------------
#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com

funWithParam(){
    
    
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    //n>=10 用${n}获取参数
    echo "第十个参数为 ${10} !"
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73


#Close all java processes ps -ef|grep java|grep -v "grep"|awk'{print $2}'|xargs kill -9
Edited under windows and uploaded to the Linux system for execution. The format of the .sh file is dos format
vi test.sh
:set ff=unix

Guess you like

Origin blog.csdn.net/u011445756/article/details/88048120