linux shell脚本写法,linux: 常用shell脚本写法
1.模拟linnux登录shellecho -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脚本写法相关推荐
- 云服务器测速脚本_VPS服务器常用性能测试脚本
购买新的VPS或者服务器都要进行一番的测试,测试机器的配置和性能,测试VPS服务器的网络速度,硬盘性能等.这里收集一些测试脚本,脚本均来自于网络,代码内有作者信息,感谢网络大神们提供这么优秀的工具. ...
- Unity脚本介绍和常用API
Unity脚本介绍和常用API 脚本介绍 1.脚本 介绍 语法结构 编译过程 2.开发工具 MonoDevelop Visual Studio 3.脚本生命周期 简介 脚本生命周期的阶段 1.初始阶段 ...
- linux过滤脚本中的字段,Shell脚本中常用的文本过滤命令
在Linux运维日常工作中,Shell脚本的使用如家常便饭一样,须做到顺手拈来,文本处理所占比重更是不容小视,而文本处理中,稍具难度的莫过于文本的过滤.今天我们要通过这一讲,掌握文本过滤的常用命令,有 ...
- Linux常用Shell脚本珍藏
我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本.如何学好脚本,最关键的 ...
- linux经典脚本实例,Linux常用Shell脚本珍藏
[实例简介] Linux常用Shell脚本珍藏 (2)编写任务定时器: C代码☆ 1. echo 00 1 *k */bin/sh /usr/bin/fileback">:/root/ ...
- Linux 常用Shell脚本,后端开发必备
Linux 常用Shell脚本,后端开发必备 文章目录 Linux 常用Shell脚本,后端开发必备 1 用户猜数字 2 查看有多少远程的 IP 在连接本机 3 helloworld 4 打印 tom ...
- linux cron记录时间,crontab每分钟,5,10分钟,每小时,每天零点,每周日0点,每月1号0点,每年1月1日执行脚本,linux shell定时任务...
crontab每分钟,5,10分钟,每小时,每天零点,每周日0点,每月1号0点,每年1月1日执行脚本,linux shell定时任务 在linux系统里做维护和开发时, 经常会遇到要用crontabl ...
- 使用 shell 脚本对 Linux 系统和进程资源进行监控
Shell 简介 Shell 语言对于接触 LINUX 的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是 ...
- linux下shell脚本论文,Linux下Shell脚本编程
1. shell脚本是什么 它是一种脚本语言,并非编程语言. 可以使用一些逻辑判断.循环等语法. 可以自定义子函数,是系统命令的集合. shell脚本可以实现自动化运维,大大增加我们的工作效率. 2. ...
- linux 定时执行搅拌,Linux上定时shell脚本
本文主要介绍在Linux系统上部署定时器,定时执行shell脚本,通过脚本执行sql文件 sql文件 -- 创建表 create table if not exists iot_test.iot_ta ...
最新文章
- Java 多线程编程之 interruptException
- python编程入门书-编程小白的第一本 Python 入门书
- PCI总线原理(二)
- 渗透学习笔记--基础篇--sql注入(数字型)
- 82.反向代理与负载均衡原理
- AJAX 跨域调用和 Java 跨域 发送请求
- 和 Pipelining 说再见,cURL 放弃使用管道技术
- NHibernate ModelBinder for mvc3
- 【转】mysql createtable errno 121 150 错误 建表错误 外键
- mac matlab破解
- 将keras的模型封装成可转换为tensorlow的.pb格式,并生成.pbtxt文件
- 5.1.2全景声音箱摆位_杜比全景声7.1.2和7.1.4音箱摆位有什么区别
- es 排序 聚合统计_ES聚合排序java
- html中电子邮件地址怎么写,电子邮件地址怎么写
- [详细教程]jdk官方下载,Linux安装jdk1.7,图文解说,一看就会
- java之集合ArrayList,LinkedList,HashMap运用
- 第六章Python数据可视化
- spider_study 1
- linux下如何启动ice服务器,linux: ICE搭建
- 软考的高级证书难考吗,难度大吗?
热门文章
- ALV的SAP自带标准程序实例
- 编写函数,可以接收任意多个整数并输出其中的最大值和所有整数之和。
- ajax html xml数据格式,AJAX | 数据格式JSON与XML的区别
- 剪映导出帧率选多少_剪映帧率怎么设置?剪映帧率设置方法
- 泰坦尼克号数据_如何使用Pandas进行可视化分析-以泰坦尼克号数据集为例
- c blowfish java,blowfish-golang加密与java解密
- php 头像拼图,php 有趣的头像拼图
- php获取ip 1,为什么php获取ip显示::1?
- 攻防世界(Pwn)dice_game, 栈溢出覆盖srand种子
- data:image/png;base64,, CTF杂项base64图片隐写-河南省第二届金盾信安杯