Simple shell learning Day1

1. Types and precautions of shell scripts

Type: cat /etc/shells
bin/bash=======>Linux uses
bin/sh
bin/csh
bin/ksh by default

Note: It is different from C and returns 0 for success and 1 for failure.
Good grammatical habit: when fetching the value of a variable, generally add "VAR"

2. Execute the script method

1.chmod a+x shell.sh ./shell.sh
2./bin/bash shell.sh
3. . shell.sh

insert image description here
3. Instructions and Syntax
A. Variables

VAR=hello -- local variable (local variable) echo $VAR env | grep &VAR has no effect
export VAR is set as environment variable (global variable) echo $VAR || env | grep &VAR has effect
alias === rename the command

B. Command Substitution

Backticks ====== ESC key below ------> replace function
VAR= date----------------------->echo $VAR ----> Call date function
( ) equivalent: VAR = () equivalent: VAR=( ) equivalent : V A R=(date)

C. Arithmetic Substitution

VAR=33,echo $((VAR+3)) == echo ( ( ((( ( VAR+3)) == echo $[ VAR+3]
echo $[ 2#10 +11]//2 base 10+(decimal) 11

D. Escape character: \

touch $\ \test.sh ---- > generate $ test.sh delete is also
touch ./-abc -abc
equivalent touch – -abc
touch ./-------xyz ------- xyz

F. Single quotes double quotes

VAR=date
echo ' V A R ′ e c h o " VAR' echo " W A Recho"VAR"

G. Conditional tests and comparison symbols

VAR=2
test $VAR -gt 1
comparison symbol
-gt is greater than or not
-lt is less than
-qt is greater than
-eq is equal to
-ne is not equal to
-d is a directory
-f is a normal directory
-p special file
-z to determine whether the string is 0
-n is not 0
H. and or not
-a is and &&
-o is ||
-x is ^
VAR=abc

F. Branch statement

if [ -d t.txt ]; then
内容
fi

Special
if: ;then echo "always true" ; fi
*** Note: means always true

test code

#! /bin/bash
echo "Is it morning? please answer yes or no"
#相当于fgets()
read YES_OR_NO
if [ "$YES_OR_NO" = "yes" ]; then
	echo "Good morning !"
elif [ "$YES_OR_NO" = "no" ]; then
	echo "Bad weather!!"
else
	echo "Sorry, $YES_OR_NO not recognize. Enter yes or no"
fi

insert image description here

case “$VAR” in
y] ;;
N] ;;
] ;;
;;---->代表break
read -p "please enther the first number n1 " n1

#! /bin/bash
echo "Is it morning? please answer yes or no"
#相当于fgets()
read YES_OR_NO
#取放到集合里面IN
case "$YES_OR_NO" in
#注意只有)2
yes|y|YES|Yes)
	echo "Good morning !";;
no|No)
	echo "Bad weather!!";;
*)
	echo "Sorry, $YES_OR_NO not recognize. Enter yes or no"
	return ;;
esac

insert image description here

Loop
foreach - traverse for/do/done
for sth in $@ ;do
done

#! /bin/bash
echo "Enter Yes or No"

for FILENAME in $(ls); do
	printf "$FILENAME "
	if [ -f "$FILENAME" ];then
		echo " "$FILENAME" is a file"
	else
		echo "It is not a file"
	fi
done 

while []; do
done

#! /bin/bash

count=3
echo "Enter password"
read TRY
while [ "$TRY" != "secret" -a $count -gt 0 ]
do
	count=$[count-1]
	echo "Sorry ,try again"
	read TRY
	
done

read

read -p 读取控制台输入的值并赋予num1
read -p "please write a num " NUM1
echo "you write num is $NUM1"
-t是在规定时间为10秒内没有输入时就会直接执行下面的语句
read -t 10  -p "please write a num " NUM2
echo "the else num is $NUM2"

G. Function construction

#! /bin/bash
#一次创建多个目录,目录名字从参数中传进取,脚本测试目录是否存在,如果不存在,先打印不存在的信息然后创建该目录

is_directory()
{
	DIR_NAME=$1

	
		#从参数中判断
		if [ ! -d $DIR_NAME ]; then
			#如果文件存在则返回
			return 1
		else
			return 0
		fi
}

#命令行参数调用

for DIR in "$@"; do
	if is_directory "$DIR";then
		echo "file have already created "
	else
		echo "$DIR doesn't exist. Create it now..."

		#标准输出重建到黑洞里面不管创建成功还是失败他都会重定向到黑洞里面

		mkdir $DIR > /dev/null 2>&1
		#创建失败则退出程序
		if [ $? -ne 0 ]; then
			echo "Can't create directory $DIR"
			exit 1
		fi
	fi

done


H. Positional parameters and special variables! ! !

$0 Get the name of the execution file
$nn takes 1-n to indicate the parameters passed in by the execution file
$# Get the total number of parameters
$* Print all parameters in one line
$@ Print n lines of n parameters for the for statement
$? Print the last execution The result of
$$ prints the PID of the executed script
shift shifts the parameter to the left
echo -n "hello\n" does not parse the escape character
echo -e "hello\n" parses the escape character
G. pipe and file redirection
tee
ps aux | grep init | tee out ====>The filtered information is stored in the out file and displayed on the screen

cmd > file
cmd >> file
cmd > file 2>&1 > without spaces after=> Redirect output as standard input=> content to file
cmd < file > file2 === < redirect the content of file to the screen and redirect to file2 inside
wc -l test_if_case.sh > out
cat < test_if_case.sh > out
cmd < &fd === =====> Treat file descriptors as standard input
cmd > &fd ========> Treat file descriptors as standard output
cmd < &- Close standard output

Guess you like

Origin blog.csdn.net/weixin_40178954/article/details/100585887