当前位置:网站首页>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
边栏推荐
- Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
- iPhone13再降价,其实只是做做样子,消费者都在等iPhone14
- Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
- [day02] Introduction to data type conversion, operators and methods
- 第4章 Bean对象的作用域以及生命周期
- Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
- 结构型模式-桥接模式
- Post analysis of Data Analyst
- playwright网络爬虫实战案例分享
- 项目参数做成可配置项,@ConfigurationProperties注解的使用
猜你喜欢

Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products

2022杭电多校联赛第三场 题解
![[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)](/img/f4/70634556c4ae8fc3b087084e1e27b3.png)
[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)

干货 | 独立站运营怎么提高在线聊天客户服务?
![Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量

Overview of communication protocols

Anonymous named pipes, understanding and use of interprocess communication in shared memory

Wechat applet rotation map

【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)

Interview must ask | what stages does a thread go through from creation to extinction?
随机推荐
Some common instructions in JVM tuning
【动态规划百题强化计划】11~20(持续更新中)
Wechat input component adds a clear icon, and clicking clear does not take effect
数据分析师岗位分析
【C语言】递归详解汉诺塔问题
Overview of communication protocols
Brightcove appoints Dan Freund as chief revenue Officer
VSCode开启Pull Request更新代码分支可视化新篇章
sram、dram、sdram、ddr的区别和用途
Why does genericservlet have two init methods
数据库泰斗王珊:努力创新,精心打磨优质的数据库产品
From scratch, C language intensive Lecture 4: array
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
Qstring conversion char*
STM32基于HAL库的串口接受中断和空闲中断
Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
微服务的feign调用header头被丢弃两种解决方案(附源码)
e.target与e.currentTarget的区别
redux三大核心
Install and configure Debian on a wired network