如何从shell脚本获取一个结果

如何从shell脚本获取一个结果,第1张

将一条命令的执行结果取得命令执行结果的值有:

1、使用反引号`` (数字键1左边的键,tab键上面,英文方式输入) 如:a=`echo "hello world"`;即将命令 echo "hello world"的输出赋给变量a

2、可以使用 $(()),如:a=$(echo "hello world"),即将命令echo "hello world"的输出赋给变量a。

以下为具体语句:

实例结果:

shell 是一个交互性命令解释器。shell独立于 *** 作系统,这种设计让用户可以灵活选择适合自己的shell。shell让你在命令行键入命令,经过shell解释后传送给 *** 作系统(内核)执行。

shell是一个命令处理器(command processor)——是一个读入并解释你输入的命令的程序。除了是一个命令中断器以外,shell还是一个程序设计语言。你可以编写shell可以解释的程序(被称为源程序),这些源程序可以包含shell程序设计命令等等。shell除了解释命令以外,还有其他工作,它也可以配置和编程。     

原因比较简单,这个脚本执行的时候,会有一个这样的进程:

sh testsh sdfsdfdsfsf

这个会被过滤出来,所以+1

其他的还有什么需要调试一下,把wc -l 下去掉,看看具体都过滤到了什么

$1 这里表示脚本的第一个参数

$# 表示脚本的参数数目

你要接受更多参数请使用 $2, $3, $4 $9,再多的话请使用shift移位。

read -p "please input your name first:" AAA 你输入的内容就是赋值给AAA的

跟 read AAA 效果一样,只是通过-p选项给出了人性化的输入提示。

一般shell只接受$0~$9十个位置参数,其中$0表示脚本名称本身,也就是说只有$1~$9共9个参数。超过9个参数的话,比如你这里要10个数,需要用shift移位来获取后面的更多参数。

#!/bin/sh

if [ $# -ne 10 ]; then

echo -e "Wrong parameters!\nYou MUST input 10 digits"

exit 1

fi

min=$1

max=$1

i=1

while [ $i -lt 10 ]

do

shift 1

let i+=1

[ $1 -lt $min ] && min=$1

[ $1 -gt $max ] && max=$1

done

echo "Min=$min"

echo "Max=$max"

exit 0

| 对于初学者而言,因为没有实战经验,写不出来 Shell 脚本 很正常,如果工作了几年的运维老年还是写不出来,那就是没主动找需求,缺乏练习,缺乏经验。针对以上问题,总结了30个生产环境中经典的 Shell 脚本 ,通过这些需求案例,希望能帮助大家提升Shell编写思路,掌握编写技巧。 |

先了解下编写Shell过程中注意事项:

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">开头加解释器:#!/bin/bash

语法缩进,使用四个空格;多加注释说明。

命名建议规则:变量名大写、局部变量小写,函数名小写,名字体现出实际作用。

默认变量是全局的,在函数中变量local指定为局部变量,避免污染其他作用域。

有两个 命令 能帮助我调试脚本:set -e 遇到执行非0时退出脚本,set-x 打印执行过程。

写脚本一定先测试再到生产上。

</pre>

1、获取随机字符串或数字

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">获取随机8位字符串:

方法1:

471b94f2

方法2:

vg3BEg==

方法3:

ed9e032c

获取随机8位数字:

方法1:

23648321

方法2:

38571131

方法3:

69024815

cksum:打印CRC效验和统计字节

</pre>

2、定义一个颜色输出字符串函数

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:

function echo_color() {

if [ 2\033[0m"

elif [ 2\033[0m"

fi

}

方法2:

function echo_color() {

case 2[0m"

;;

red)

echo -e "[31;40m$2[0m"

;;

)

echo "Example: echo_color red string"

esac

}

使用方法:echo_color green "test"

function关键字定义一个函数,可加或不加。

</pre>

3、批量创建用户

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash

DATE= 1 == "green" ]; then

echo -e "[32;40m 1 == "red" ]; then

echo -e "[31;40m$2[0m"

fi

}

if [ -s USER_FILE {DATE}bak

echo_color green " {USER_FILE}- USER_FILE

echo "----------------" >> USER &>/dev/null; then

PASS= RANDOM |md5sum |cut -c 1-8)

useradd PASS |passwd --stdin USER USER_FILE

echo " USER User already exists!"

fi

done

</pre>

4、检查软件包是否安装

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash

if rpm -q sysstat &>/dev/null; then

echo "sysstat is already installed"

else

echo "sysstat is not installed!"

fi

</pre>

5、检查服务状态

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">#!/bin/bash

PORT_C= (ps -ef |grep ntpd |grep -vc grep)

if [ PS_C -eq 0 ]; then

echo "内容" | mail -s "主题" dst@examplecom

fi

</pre>

6、检查主机存活状态

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">方法1:将错误IP放到数组里面判断是否ping失败三次

IP_LIST="192168181 19216811 192168182"

for IP in NUM -le 3 ]; do

if ping -c 1 IP Ping is successful"

break

else

# echo " NUM"

FAIL_COUNT[ IP

let NUM++

fi

done

if [ {FAIL_COUNT[1]} Ping is failure!"

unset FAIL_COUNT[]

fi

done

方法2:将错误次数放到FAIL_COUNT变量里面判断是否ping失败三次

IP_LIST="192168181 19216811 192168182"

for IP in IP >/dev/null; then

echo " IP Ping is failure FAIL_COUNT -eq 3 ]; then

echo "$IP Ping is failure!"

fi

done

方法3:利用for循环将ping通就跳出循环继续,如果不跳出就会走到打印ping失败

ping_success_status() {

if ping -c 1 IP Ping is successful"

continue

fi

}

IP_LIST="192168181 19216811 192168182"

for IP in IP Ping is failure!"

done

</pre>

7、监控CPU、内存和硬盘利用率

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)CPU

借助vmstat工具来分析CPU统计信息。

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (vmstat |awk NR==3{print (( SY))

if [ DATE

Host: USE

" | mail -s "CPU Monitor" $MAIL

fi

2)内存

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (free -m |awk /Mem/{print (free -m |awk /Mem/{print 6- (( USE))

if [ DATE

Host: TOTAL,Use= FREE

" | mail -s "Memory Monitor" $MAIL

fi

3)硬盘

DATE= (ifconfig eth0 |awk -F [ :]+ /inet addr/{print (fdisk -l |awk -F [: ]+ BEGIN{OFS="="}/^Disk /dev/{printf "%s=%sG,", 3} )

PART_USE= 1,int( 6} )

for i in (echo (echo (echo USE -gt 80 ]; then

echo "

Date: IP

Total: PART= MOUNT)

" | mail -s "Disk Monitor" $MAIL

fi

done

</pre>

8、批量主机磁盘利用率监控

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">前提监控端和被监控端SSH免交互登录或者密钥登录。

写一个配置文件保存被监控主机SSH连接信息,文件内容格式:IP User Port

HOST_INFO=hostinfo

for IP in 1} (awk -v ip= 1{print HOST_INFO)

PORT= IP ip== 3} PORT IP df -h > (awk BEGIN{OFS="="}/^/dev/{print 5)} USE_RATE_LIST; do

PART_NAME= {USE_RATE#=}

if [ PART_NAME Partition usage $USE_RATE%!"

fi

done

done

</pre>

9、检查网站可用性

<pre style="margin: 0px; padding: 0px; overflow: auto; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">1)检查URL可用性

方法1:

check_url() {

>

以上就是关于如何从shell脚本获取一个结果全部的内容,包括:如何从shell脚本获取一个结果、shell脚本输入一个$1变量执行count值为什么是2,但是如果把$1替换为字符串,count值就正常了,急急急!、shell脚本不懂了,指教!!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9622581.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存