shell example01
条件判断
if [[ -e ${1} ]]; then
echo "$(tput setaf 2) found ${1} $(tput sgr0)"
cat ${1}
else
echo "$(tput setaf 1) not found ${1} $(tput sgr0)"
exit 1
fi
//简化
[[ -e ${1} && -e ${2} ]] && cat ${1} > ${2}
//判断取反
txt='4.txt'
if ! [[ -e ${txt} ]]; then
touch ${txt}
echo "touch ${txt}; name length ${#txt}"
fi
//多重选择
bold=$(tput bold)
underline=$(tput smul)
normal=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
if [[ -e 'a1.txt' ]]; then
echo "${bold}${underline}${green} search a{1..5}.txt ${normal}"
ls a{1..5}.txt 2> log.txt
elif [[ -e 'b1.txt' ]]; then
echo "${bold}${underline}${green} search b{1..5}.txt ${normal}"
ls b{1..5} txt 2>> log.txt
else
echo "${bold}${underline}${red} create a{1..4}.txt ${normal}"
touch a{1..5}.txt
fi
显示时间
//查找文件
start=$(date +%s)
echo 'start search'
find ~/ -name '*.txt' &> log.txt
echo 'end search'
end=$(date +%s) //%s以秒为单位
difference=$(( end - start))
echo "Time is ${difference} seconds".
//显示具体的执行时间
echo 'start search'
time find ~/ -type f -name '*txt' &> log.txt //仅仅是文件
echo 'end search'
//计时器
echo '- Count Start'
tput sc //存储光标位置
count=0
while [[ ${count} -lt 20 ]]
do
let count++
sleep 1 //延迟一秒
tput rc //恢复光标位置
tput ed //清除从当前光标位置到行尾之间的所有内容
echo "- ${count}"
done
echo "- Count End"
循环
//循环创建文件
for i in file{1..9}.txt
do
if [[ -e ${i} ]]
then
echo "find ${i} skip"
else
echo "start create ${i}"
touch ${i}
fi
done
echo "finished"
//判断空文件
files=$(echo *.txt)
normal=$(tput sgr0)
type="%-10s %-8s\n"
printf "$(tput bold)${type}" name blank
printf ${normal}
for file in ${files}; do
check=true
color=$(tput setaf 1)
test -s ${file} && check=false color=$(tput setaf 2)
printf "${color}${type}" ${file} ${check}
printf ${normal}
done
简单编辑
//去除空白
cat $1 | sed '/s //g' > new.log //sed 's/要被取代的字串/新的字串/g'
//对特定格式的文档计算重复值
//log.txt
account1:passwd1
account2:passwd2
account3:passwd3
account2:passwd4
account4:passwd5
account6:passwd6
account1:passwd7
//
awk -F: 'x[$1]++ {print $1 " is duplicated"}' log.txt //awk -F 指定分割字符
shell example01的更多相关文章
- Shell 基础笔记
1-22-shell脚本的基础 本节所讲内容: shell 基本语法 变量 第1章 什么是SHELL?.. 2 1.1 shell编程.. 3 第2章 shell变量及运用.. ...
- Shell替换
如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. #!/bin/bash a= echo -e "Value of a is ...
- Shell特殊变量
$ 表示当前Shell进程的ID,即pid $echo $$ 运行结果 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数 ...
- shell变量
定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同时,变量名 ...
- 第一个shell脚本
打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...
- shell简介
Shell作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支. shell使用的熟练程度反映了用户对U ...
- Shell碎碎念
1. 字符串如何大小写转换 str="This is a Bash Shell script." 1> tr方式 newstr=`tr '[A-Z]' '[a-z]' < ...
- MongoDB学习笔记二—Shell操作
数据类型 MongoDB在保留JSON基本键/值对特性的基础上,添加了其他一些数据类型. null null用于表示空值或者不存在的字段:{“x”:null} 布尔型 布尔类型有两个值true和fal ...
- 使用C#给Linux写Shell脚本
在这个逼格决定人格,鄙视链盛行的年头,尤其是咱们IT界,请问您今天鄙视与被鄙视的次数分别是多少?如果手中没有一点压箱的本事,那就只有看的份了.今天我们也要提升下自己的格调,学习些脑洞大开的东西,学完之 ...
随机推荐
- Alpha阶段第三次Scrum Meeting
情况简述 Alpha阶段第三次Scrum Meeting 敏捷开发起始时间 2016/10/24 00:00 敏捷开发终止时间 2016/10/25 00:00 会议基本内容摘要 总结了之前学习的内容 ...
- html页面输入框input的美化
input输入框是网页必不可少的组件,可是每个浏览器对于输入框的显示样式各有不同 例如: 上图分别就是谷歌浏览器和IE浏览器自带显示的输入框,样式也不足人意,所以大多都会自己写样式 以下是一个简 ...
- ubuntu下升级R版本
ubuntu下升级R版本 在测试<机器学习 实用案例解析>一书的邮件分类代码时,windows系统下rstudio中无法读取特殊字符,在ubuntu下可以.在ubuntu虚拟机下安装t ...
- 手机端多种分享plugin插件地址
//qq cordova plugin add https://github.com/iVanPan/Cordova_QQ.git --variable QQ_APP_ID=app_id 参考文档 h ...
- WebService学习总结(四)——调用第三方提供的webService服务
http://www.cnblogs.com/xdp-gacl/p/4260627.html 互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他 ...
- request 获取服务根目录地址
这是常用的request获取服务地址的常用方式. 源请求服务地址:http://localhost/api-server/1/forum/thread/hot_topic?sex=1 String p ...
- mysql命令总结
统计全库数据量: use information_schema; SELECT TABLE_NAME, (DATA_LENGTH) as DataM , (INDEX_LENGTH) as Index ...
- js轮播(qq幻灯片效果)
<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equ ...
- Shader的学习方法总结
最近网友candycat1992的新书<Unity Shader入门精要>出版了,估计万千的中国unity开发者又要掀起一波学Shader热潮了.我也想把自己这几年学习Shader的一些历 ...
- css 实现三角形 实现过程
1.纯色的全等的三角形实现 下面的就是实际实现 没有宽高 只有边框 都是透明 根据箭头的方向 给边框方法加颜色 比如需要像右箭头 只需要给border-right-color:颜色值; 即可 c ...