当前位置: 首页 > 编程笔记 >

自己常用的一些shell脚本分享

屠昌胤
2023-03-14
本文向大家介绍自己常用的一些shell脚本分享,包括了自己常用的一些shell脚本分享的使用技巧和注意事项,需要的朋友参考一下

自己写了一下小的shell实例,虽然很小,但所有的大的程序都是由小的模块堆积起来的,程序员一定要懂得一种脚本的书写,而我,只会在linux下工作,所以就只能写linux的shell脚本了,呵呵,本文会陆续更新,给自己加油!

1.模拟linnux登录shell


#/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


2.比较两个数大小

#/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.打印当前用户

#/bin/bash

echo "Current User is :"

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


20.case语句练习

#!/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返回不同的结构

#!/bin/bash

clear

echo "enter [y/n]:"

read a

case $a in

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

    ;;

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

    ;;

    *) echo "error"

    ;;

esac

22.内置命令的使用


#/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.打印无密码用户


#/bin/bash

echo "No Password User are :"

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


24.

#/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.检查端口号是否已启动


#!/bin/bash

n=1

echo "检查xxx服务..."

while true

do

        if test $n -gt 20

        then 

                echo "xxx服务启动失败"

                break

        fi

                

        sleep 5

        n=$(($n+1))

        port=`netstat -antp | grep "0.0.0.0:8080"`

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

                echo "xxx服务已经启动"

                break;

        fi

done

 类似资料:
  • 本文向大家介绍Linux下一些常用的Shell脚本整理,包括了Linux下一些常用的Shell脚本整理的使用技巧和注意事项,需要的朋友参考一下 如何计算当前目录下的文件数和目录数                 如何只列子目录?             如何实现取出文件中特定的行内容        如果你只想看文件的前5行,可以使用head命令,        如:          如果你想查看

  • 一、解放生产力 以下脚本需要放入一个可执行文件中,实现双击就可以运行! 1.1、快速提交代码 # Mac 上可以去掉脚本的第一行标志:「#!/bin/bash」 # 加上这个,颜色码会失效 # 1、cd 到当前目录 currentDir=$(cd "$(dirname "$0")"; pwd) cd ${currentDir} # 2、获取当前的分支 currentBranch=$(git s

  • 本文向大家介绍shell脚本中常见的一些特殊符号和作用详解,包括了shell脚本中常见的一些特殊符号和作用详解的使用技巧和注意事项,需要的朋友参考一下 在编写Shell脚本时,我们需要会用到各种各样的特殊符号,通过这些特殊符号可以使我们编写的代码更加简洁和高效,这里给大家汇总下: 1、{} 大括号: 用法一:通配符扩展 eg: ls my_{finger,toe}s 这条命令相当于如下命令的组合:

  • 第一个shell脚本 编写 打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell 脚本,扩展名就用php好了。 输入一些代码,第一行一般是这样: #!/bin/bash #!/usr/bin/php “#!”是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行。 运行 运行Shell脚本有两种方法: 作为可执行程序

  • 本文向大家介绍自己封装的常用javascript函数分享,包括了自己封装的常用javascript函数分享的使用技巧和注意事项,需要的朋友参考一下 都是些常用的功能,这里就不多废话了,小伙伴们自己看下就明白了 奉上代码:

  • 本文向大家介绍Oracle重建索引Shell脚本、SQL脚本分享,包括了Oracle重建索引Shell脚本、SQL脚本分享的使用技巧和注意事项,需要的朋友参考一下 索引是提高数据库查询性能的有力武器。没有索引,就好比图书馆没有图书标签一样,找一本书自己想要的书比登天还难。然而索引在使用的过程中,尤其是在批量的DML的情形下会产生相应的碎片,以及B树高度会发生相应变化,因此可以对这些变化较大的索引进