Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used shell scripts

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "commonly used shell scripts", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what are the commonly used shell scripts" bar!

1. Log in to shell by simulating linnux

The code is as follows:

# / bin/bash

Echo-n "login:"

Read name

Echo-n "password:"

Read passwd

If [$name = "cht"-a $passwd = "abc"]; then

Echo "the host and password is right!"

Else echo "input is error!"

Fi

two。 Compare the size of two numbers

The code is as follows:

# / bin/bash

Echo "please enter two number"

Read a

Read b

If test $a-eq $b

Then echo "NO.1 = NO.2"

Elif test $a-gt $b

Then echo "NO.1 > NO.2"

Else echo "NO.1

< NO.2" fi 3.查找/root/目录下是否存在该文件 代码如下: #/bin/bash echo "enter a file name:" read a if test -e /root/$a then echo "the file is exist!" else echo "the file is not exist!" fi 4.for循环的使用 代码如下: #/bin/bash clear for num in 1 2 3 4 5 6 7 8 9 10 do echo "$num" done 5. 代码如下: #/bin/bash echo "Please enter a user:" read a b=$(whoami) if test $a = $b then echo "the user is running." else echo "the user is not running." fi 6.删除当前目录下大小为0的文件 代码如下: #/bin/bash for filename in `ls` do if test -d $filename then b=0 else a=$(ls -l $filename | awk '{ print $5 }') if test $a -eq 0 then rm $filename fi fi done 7.如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G 代码如下: #/bin/bash while line=`ls /export/um_lpp_source` do if test $line="" then echo "NULL" sleep 1 else echo $line chfs -a size=3G /export/um_lpp_source exit 0 fi done 8.测试IP地址 代码如下: #/bin/bash for i in 1 2 3 4 5 6 7 8 9 do echo "the number of $i computer is " ping -c 1 192.168.0.$i done 9.如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件 代码如下: #/bin/sh a=2 while name="test.log" do sleep 1 b=$(ls -l $name | awk '{print $5}') if test $b -ge $a #then echo "OK" then `cp /opt/*.tar.gz .` exit 0 fi done 10.打印读取的内容,为下面的例子做准备 代码如下: #/bin/bash while read name do echo $name done 11.从0.sh中读取内容并打印 代码如下: #/bin/bash while read line do echo $line done < 0.sh 12.读取a.c中的内容并做加1运算 代码如下: #/bin/bash test -e a.c while read line do a=$(($line+1)) done < a.c echo $a 13.普通无参数函数 代码如下: #/bin/bash p () { echo "hello" } p 14.给函数传递参数 代码如下: #/bin/bash p_num () { num=$1 echo $num } for n in $@ do p_num $n done 15.创建文件夹 代码如下: #/bin/bash while : do echo "please input file's name:" read a if test -e /root/$a then echo "the file is existing Please input new file name:" else mkdir $a echo "you aye sussesful!" break fi done 16.获取本机IP地址 代码如下: #/bin/bash ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g' 17.查找最大文件 代码如下: #/bin/bash a=0 for name in *.* do b=$(ls -l $name | awk '{print $5}') if test $b -ge $a then a=$b namemax=$name fi done echo "the max file is $namemax" 18.查找当前网段内IP用户,重定向到ip.txt文件中 代码如下: #/bin/bash a=1 while : do a=$(($a+1)) if test $a -gt 255 then break else echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') echo $ip >

> ip.txt

Fi

Done

19. Print the current user

The code is as follows:

# / bin/bash

Echo "Current User is:"

Echo $(ps | grep "$$" | awk'{print $2}')

20.case statement exercise

The code is as follows:

#! / bin/bash

Clear

Echo "enter a number from 1 to 5:"

Read num

Case $num in

1) echo "you enter 1"

2) echo "you enter 2"

3) echo "you enter 3"

4) echo "you enter 4"

5) echo "you enter 5"

*) echo "error"

Esac

21.yes/no returns a different structure

The code is as follows:

#! / bin/bash

Clear

Echo "enter [yzone]:"

Read a

Case $an in

Y | Y | Yes | YES) echo "you enter $a"

N | N | NO | no) echo "you enter $a"

*) echo "error"

Esac

twenty-two。 Use of built-in commands

The code is as follows:

# / bin/bash

Clear

Echo "Hello, $USER"

Echo

Echo "Today's date id `date`"

Echo

Echo "the user is:"

Who

Echo

Echo "this is `uname-s`"

Echo

Echo "that's all folks!"

23. Print users without passwords

The code is as follows:

# / bin/bash

Echo "No Password User are:"

Echo $(cat / etc/shadow | grep "!!" | awk 'BEGIN {FS= ":"} {print $1}')

24.

The code is as follows:

# / bin/bash

Clear

Echo "Hello, $USER"

Echo

Echo "Today's date id `date`"

Echo

Echo "the user is:"

Who

Echo

Echo "this is `uname-s`"

Echo

Echo "that's all folks!"

25. Check whether the port number has been started

The code is as follows:

#! / bin/bash

Nasty 1

Echo "check xxx service..."

While true

Do

If test $n-gt 20

Then

Echo "xxx service startup failed"

Break

Fi

Sleep 5

Nasty $(($nasty 1))

Port= `netstat-antp | grep "0.0.0.0pur8080" `

If [${# port}-gt 3]; then

Echo "xxx service has started"

Break

Fi

Done

Thank you for your reading, these are the contents of "what are the commonly used shell scripts?" after the study of this article, I believe you have a deeper understanding of what the commonly used shell scripts have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report