shell编程
shell脚本编程
人保雷佳音
91后,硕士研究生,阿里云云计算高级架构师。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
(十二)Shell的行定位命令sed和排序命令sort
1、sed -n'2'p file 只打印第二行2、sed -n'2,4'p file 打印二到四行3、sed '3,6'd file 删掉第三行到第六行(不会删除原文件内容)4、sed -n'/los/'p file 打印匹配los的行5、sort file 把文件按字母的升序进行排序6、cat file|sort -t: -k1 -r 进行分割后的第一列按倒序排序...原创 2020-09-20 11:26:36 · 1237 阅读 · 0 评论 -
(十一)Shell的grep搜索命令和awk命令
grep -c 'is' file.txt 有多少行有isgrep -in 'is' file.txt 把is标记出来,忽略大小写grep -vin 'linux' file.txt 不包含linux的行grep结合正则表达式详解 1、grep -E '^linux' file.txt 以linux开头的行2、grep -E 'php$' file.txt 以php结尾的行3、grep -E '.+linux.+' file.txt li...原创 2020-09-17 20:39:55 · 438 阅读 · 0 评论 -
(十)Shell的Find查找命令详解
1、find -name '*.sh' 找以sh结尾的文件2、find -name "test[1-6]*" 找以字母开头的文件3、find /etc -name "host*"4、find -perm 755 查755权限的文件5、find -user root 在当前目录下属主为root的文件6、find /var -mtime -5 找更改时间在5天内的文件7、find /var -mtime +3|wc -l 找更改时间在3天前的文件的个数8、find t...原创 2020-09-16 21:03:20 · 3112 阅读 · 0 评论 -
(九)Shell循环控制和函数
while和for循环while循环 手动输入一个值,求0到该值的累加和#!/bin/bash#test9.shread -p "input a number,we will sum 0 to this number:" numbertotal=0while [ $number -ge 0 ]do echo $number total=$(($total+$number)) number=$(($number-1)) ...原创 2020-09-14 19:58:42 · 182 阅读 · 0 评论 -
(八)Shell流程判断
主要分为if和case两大部分。if语句#!/bin/bash#test6read -p "please input a number:" numberif [ $number -lt 20 ]then echo "小于20"elif [ $number -lt 10 ]then echo "小于10"else echo "大于等于20"fi case语句 判断周几的shell#!/bin/ba...原创 2020-09-14 15:52:35 · 160 阅读 · 0 评论 -
(七)Shell条件测试
分为:文件测试、整型测试、字符串测试、逻辑测试文件测试#!/bin/bash#test.shif [ -d /root/shellTest ]then echo "this is a 目录"else echo "这不是目录"fi[ -d /root/shellTest ] #是否为目录[ -f /root/shellTest/test1.sh ] #是否为文件-e 目录或文件是否存在-r 当前用户是否有权限读取-w 当前用户是否有权限写入-x 当..原创 2020-09-14 10:40:11 · 196 阅读 · 0 评论 -
(六)Shell输入、输出功能
普通输入输出#!/bin/bashecho -e "my name is\nJack"echo -n "please input your name:"read nameecho "His name is $name" 其他输出命令cat、tee、more、head、tail、nl1、cat <<x aaaaaaaaaaaaa x2、tee 输出结果的同时保存到name.txt sh test8.sh|tee n...原创 2020-09-11 15:11:56 · 205 阅读 · 0 评论 -
(五)Shell位置变量、预定义变量和运算符
位置变量 新建test4.sh,测试$1,传的第一个变量。#!/bin/bash#test.shcase $1 in start) echo 'start....' ;; stop) echo 'stop....' ;;esac 执行 sh test4.sh start预定义变量新建test5.sh ,测试$0 、$*、 $##!/bin/bash#te...原创 2020-09-11 09:49:53 · 128 阅读 · 0 评论 -
(四)Shell中变量的使用
新建test3.sh文件,输入以下代码#!/bin/bashread -p "please input your name:" nameecho "my name is user$name"echo "my name is ${name}_Cheng"执行该sh文件,sh test3.sh键入名字Jack原创 2020-09-10 16:13:47 · 126 阅读 · 0 评论 -
(三)Shell脚本简单示例
写一个定时任务每周五17:30,把日期和当前目录文件写入clean.log文件中。viftpclean.sh#!/bin/bashdate >>clean.logls>>clean.logcrontab -e30 17 * * 5 /root/shellTest/ftpclean.sh写一个脚本文件,查询磁盘空间、内存空间、查询普通用户列表。#!/bin/bash#test1.shecho 'disk space'echodf...原创 2020-09-10 09:06:48 · 172 阅读 · 0 评论 -
(二)Shell文件权限
一、查看文件属性ls -l filename-rw-r--r-- 文件或目录、所有者、所属组、其他用户二、用户管理、用户组 用户添加 useradd user1 用户删除 userdel -r user1 查询用户 id user1 修改用户密码 passwd user1 把用户加入组 gpasswd -a user1 root 把组中用户删除 gpasswd -d user1 root三、用户与文件...原创 2020-09-09 11:58:23 · 707 阅读 · 0 评论 -
(一)Shell重定向和管道操作
重定向 新建一个test.sh,键入两行shell,一行是正确的命令,一行是错误的命令。#!/bin/bash#test.shls -llllll 执行sh test.sh,利用不同的重定向会产生不同的效果。 sh test.sh >>root.txt --追加到尾部 sh test.sh >root.txt --把原来文件中清空然后输入 sh test.sh 2>root.txt --如果命令执行是错误的会把结果...原创 2020-09-08 09:48:06 · 325 阅读 · 0 评论
分享