当前位置:网站首页>Shell programming enhancements
Shell programming enhancements
2022-07-27 04:38:00 【Light chasing rain】
List of articles
One shell
[3] shell Use of judgment in
(1) Judgment of numbers
-eq be equal to
-ne It's not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
(2) Logical operations
&& ---> -a
|| ---> -o
! ---> !
(3) Judgment string
-z Determines if the string is empty
-n Judge whether the string is not empty
= perhaps == Determines whether the strings are equal
!= Determines whether the strings are unequal
\> Judge whether it is greater than
\< Judge whether it is less than
(4) Judgment of file type
-e: Judge whether the file exists
-s: Judge whether the file exists , And determine whether the file is empty
-b: Judge whether the file exists , And determine whether the file is a block device file
-c: Judge whether the file exists , And determine whether the file is a character device file
-d: Judge whether the file exists , And determine whether the file is a directory
-f: Judge whether the file exists , And determine whether the file is an ordinary file
-L: Judge whether the file exists , And determine whether the file is a linked file
-S: Judge whether the file exists , And determine whether the file is a socket file
-p: Judge whether the file exists , And determine whether the file is a pipeline file
(5) Judgment of file permissions
-r: Judge whether it has readable permission
-w: Determine whether you have writable permission
-x: Judge whether it has executable permission
#!/bin/bash
read -p "input filename> " file
if test -e "$file" -a -n "$file"
then
if test -L $file
then
echo " This is the linked file "
elif test -f $file
then
echo " This is an ordinary file "
elif test -d $file
then
echo " This is a directory "
elif test -c $file
then
echo " This is the character device file "
elif test -s $file
then
echo " This is the socket file "
elif test -p $file
then
echo " This is a g Pipeline files "
fi
else
echo "file not found ,please check input filename"
fi
practice `
Enter the name of a file into the script , Judge whether the input is not empty , If it's not empty , Judge whether it is a normal document If it's a regular document , Again
Judge whether the file has write permission. If the file does not have write permission, add write permission , Append... To the end of the file hello world this sentence .
#!/bin/bash
read -p "input filename >" file
if [ -n "$file" ]
then
if [ -f "$file" ]
then
if [ ! -w "$file" ]
then
chmod u+w $file
fi
echo “hello world” >> $file
else
echo " The input is not an ordinary file "
exit 1
fi
else
echo "input try again"
exit 1
fi
[4] case in sentence
case Variable in
Matching options 1)
sentence 1;
Matching options 2)
sentence 2;
Matching options n)
sentence n;
Format of matching options :
*: Match any character
[ character 1 character n]: matching [] Any character inside
“y”|"yes" : If more than one can be used |
,|.|:|\"|?: Note the user escape character
#!/bin/bash
read -p "input number >" num
case $num in
1)
echo " Monday "
;;
2)
echo " Tuesday "
;;
*)
echo " Input error "
;;
esac
#!/bin/bash
read -p "input char > " cc
case $cc in
[0-9]|"10")
echo " This is a number "
;;
[a-zA-Z])
echo " It's a letter "
;;
,|.|:|\"|?)
echo " This is a punctuation mark "
;;
*)
echo " Input error "
;;
esac
[5] Loop statement
(1)while loop
while The loop condition
do
The loop body
done
#!/bin/bash
sum=0;
i=0;
while [ $i -le 100 ]
do
((sum += i++))
done
echo $sum
practice 2
Create two directories under the user's home directory file-dir and dir-dir
If the directory already exists , There's no need to create , Ask the user if you need to empty the directory [Y/N] y n,yes no
Enter a path , Determine if this path exists , If there is , Copy the files in this directory to file-dir, Copy directory to
dir-dir Next
Count the number of copied files and directories , Print out the number of files and directories
#!/bin/bash
arr=("/home/jsetc/file-dir" "/home/jsetc/dir-dir")
i=0
while test $i -lt ${
#arr[@]}
do
if [ -d ${arr[$i]} ]
then
echo -n " Whether it is necessary to empty ${arr[$i]} Catalog 【Y/N】}"
read chose
case $chose in
"Y"|"y"|"yes"|"YES")
echo " Emptying ${arr[$i]} in ..."
rm -rf ${arr[$i]}/*
echo "$arr[$i] It's been emptied ..."
;;
"N"|"n"|"no"|"NO")
echo "${arr[$i]} The contents of the directory have not been emptied ..."
;;
*)
echo " Input error "
;;
esac
else
mkdir ${arr[$i]}
echo " New ${arr[$i]} This directory "
fi
((i++))
done
while true
do
file=0
dir=0
read -p " Enter the path to operate >" path
if [ -d $path ]
then
arr1=(`ls $path`)
j=0
while test $j -lt ${
#arr1[@]}
do
if [ -f $path/${arr1[$j]} ]
then
echo " Copying ${arr1[$j]} file ..."
cp $path/${arr1[$j]} ${arr[0]}
((file++))
elif [ -d $path/${arr1[$j]} ]
then
echo " Copying ${arr1[$j]} Catalog ..."
cp $path/${arr1[$j]} ${arr[1]} -r
((dir++))
fi
((j++))
done
elif [ "$path" = "quit" ]
then
echo " Exit procedure ..."
exit 1
else
echo " The path entered does not exist , Please try again ..."
continue
fi
echo " Number of copied files :$file"
echo " Number of copied directories :$dir"
done
(2)for loop
1. usage 1(c style )
for((i = 0 ; i < 100;i++))
do
The loop body
done
1. usage 2(shell Peculiar for loop )
for Variable in List of words
do
The loop body
done
Be careful : Format of word list
Format 1: Ubuntu windows ios redhat
Format 2: `ls`
Format 3:{start..end}
Format 4: If in And the following word list is the default , At this time, the word list is the parameter of the command line
#!/bin/bash
for var in ubuntu windows ios iphone
do
echo $var
done
#!/bin/bash
for var in `ls`
do
echo $var
done
#!/bin/bash
:<<EOF
sum=0
for var in {
1..100}
do
((sum += var))
done
echo $sum
EOF
for var in {
A..Z}
do
echo $var
done
1. select in Loop statements that enhance interaction
select Variable in List of words
do
The loop body
done
#!/bin/bash
select var in windows ubuntu ios Android
do
echo $var
done
#!/bin/bash
select var in windows ubuntu ios Android
do
case $var in
"windows")
echo "welcome to windows"
;;
"ubuntu")
echo "welcome to ubuntu"
;;
"ios")
echo "welcome to ios"
;;
"Android")
echo "welcome to Android"
;;
*)
echo "error..."
;;
esac
done
[6] break and continue
and c The rules of language grammar are the same , The syntax format is different
break n sign out n Layer cycle
continue n skip n Layer cycle , Execute next cycle
[7] goto
It is generally not recommended to use , Generally used for error handling
边栏推荐
猜你喜欢

Okaleido tiger will log in to binance NFT in the second round, or continue to create sales achievements

Maximum value, minimum value, bubble sort in the array

使用kubesphere图形界面dashboard开启devops功能

e. The difference between target and e.currenttarget

第4章 Bean对象的作用域以及生命周期

Dino paper accuracy, and analyze the variant of its model structure & Detr

Chapter 6: cloud database

可视化领域 SVG

【C语言】递归详解汉诺塔问题

People don't talk much, engineers don't talk much
随机推荐
Title: there is an array that has been sorted in ascending order. Now enter a number and ask to insert it into the array according to the original rule.
e. The difference between target and e.currenttarget
ELS compatibility DC, transfer pictures to window
微信小程序编辑头像
Yolov4网络详解
RN开发系列<9>--Mobx(1)入门篇
Convolution neural network -- convolution of gray image
e.target与e.currentTarget的区别
The project parameters are made into configurable items, and the @configurationproperties annotation is used
Navicat exports Mysql to table structure and field description
Horizon sunrise X3 PI (IV) running on board (unfinished)
els_ Rectangle drawing, code planning and backup
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
2022杭电多校联赛第三场 题解
Overview of communication protocols
安全第四次课后练习
The difference between ArrayList and LinkedList
5.component动态组件的展示
Standard C language 13
电商分账系统重要吗,平台应该如何选择分账服务商呢?