字符串替换、赋值
yesterday=2018-06-22
echo $yesterday
temp=`echo $yesterday | sed 's/-//g'`
echo $temp
注意要使用反单引号,输出
2018-06-22 20180622@H_301_28@统计shell脚本耗时
#/bin/bash
begin_time=$(date +%s)
sleep 4
echo -e "begin_time = $begin_time"
end_time=$(date +%s)
cost_time=$(($end_time - $begin_time))
echo "执行数据一共花费了$cost_time秒"
解压tar文件
解压
tar -xf 123.tar
压缩
tar zcvf FileName.tar.gz DirName
Linux如何在vi里搜索关键字
vim打开某个文件后,输入/即可进行查找关键字,再输入n可查找下一条关键字记录
Vim的使用
目标:把一个每行都是url的txt文件内容,转为一个java代码中使用的string[]数组
@H_404_83@源Url
telepathy.kakamobi.com ai.kakamobi.cn tpc.kakamobi.cn dspmnt.autohome.com.cn dealer2.autoimg.cn comm.app.autohome.com.cn x.autoimg.cn auto.kuailemm.com online.kuailemm.com 3conline.kuailemm.com m-api.xcar.com.cn image.xcar.com.cn sa-data.xcar.com.cn m.yiche.com api.i.yiche.com api.i.bitauto.com api.ycapp.yiche.com i.api.autohome.com.cn m.autohome.com.cn www2.autoimg.cn data.autohome.com.cn app2.autoimg.cn cheyouquan.kakamobi.com 789.kakamobi.cn tiku.jiakaobaodian.com web.app.kakamobi.cn
打开终端,输入vi,进入vim编辑器,输入i,进入编辑模式,粘贴以上地址
然后ESC退出编辑模式
输入冒号,再输入以下命令,作用是给每行url最前面添加引号%s/^/"
再输入冒号,再输入以下命令,作用是给每行url最后面添加引号和逗号
%s/\n/",
然后就会得到以下字符串,达成目标
"telepathy.kakamobi.com","ai.kakamobi.cn","tpc.kakamobi.cn","dspmnt.autohome.com.cn","dealer2.autoimg.cn","comm.app.autohome.com.cn","x.autoimg.cn","auto.kuailemm.com","online.kuailemm.com","3conline.kuailemm.com","m-api.xcar.com.cn","image.xcar.com.cn","sa-data.xcar.com.cn","m.yiche.com","api.i.yiche.com","api.i.bitauto.com","api.ycapp.yiche.com","i.api.autohome.com.cn","m.autohome.com.cn","www2.autoimg.cn","data.autohome.com.cn","app2.autoimg.cn","cheyouquan.kakamobi.com","789.kakamobi.cn","tiku.jiakaobaodian.com","web.app.kakamobi.com",
http://www.cnblogs.com/Dennis-mi/articles/5939635.html
vim的其他常用命令
vim longcontent.txt
输入以下命令可显示行号
:set nu
shift + g :跳到文件末尾
shift + a:跳到行尾,并处于编辑模式
shift + $:跳到行尾,并处于命令模式,按x删减最后的内容
gg:跳到文件头
具体行数 + gg,比如222gg:跳转到指定行
shift + 4:跳到行尾
0:跳到行首
dd:删除当前行ls -l //查看文件具体详细信息列表 ls -lh //查看具体信息,文件大小用对应单位表示,不像上面那个只用字节数表示 pwd // 查看当前目录
Shell的使用
stat:查看文件的详细信息,包括创建时间,改动时间等 history:显示所有已经输过的命令 rm -rf:所到之处,寸草不生的永久删除无法恢复的命令,建议修改bashrc改造成mv命令 mv -b:移动前如果存在就先备份(简单备份) \mv --backup=numbered 11.txt /trash:选择备份策略,backup的参数值有4个: none或off : 不备份 numbered或t:数字编号的备份 existing或nil:如果存在以数字编号的备份,则继续编号备份m+1...n:如果之前没有以数字编号的文件,则使用下面讲到的简单备份。 simple或never:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。 alias:查看所有别名 \mv:不使用别名 $@:是传给脚本的所有参数的列表 $1 是传递给该shell脚本的第一个参数 df -lh:查看剩余空间 tail -n 100 xxx.txt:查看xxx.txt文件最后一百行 head xxx.txt:查看xxx.txt前10行 ps -aux | grep java:查看java进程 ps -ef |grep 123.sh:查看123.sh的进程号及父进程 crontab:定时器 * * * * * command 分 时 日 月 周 命令 */30 * * * * date >> /root/mycrontab:每半小时向mycrontab写入一个当前时间字符串 crontab crontabname:执行这个定时器 /etc/init.d/crond restart:重启crontab /var/spool/cron/用户名:定时器位置 crontab -r:删除crontab crontab -e:修改crontab
find /home -name "tosql.sh"
find命令,可以用来制作回收站的清空功能,参考 Linux回收站功能的实现
find /trash -mtime +7 -name "*.*" -exec rm -rf {} \ 找到并删除trash目录下7天前所有带.的文件及目录 * 3 * * * find /trash/* -mtime +7 -exec rm -rf {} \; 每天三点删除trash目录下7天以前的数据 * 3 * * * find /trash/* -mtime +7 -fls /home/crontab/trash.txt -delete 此命令的意义是每天三点找到并删除/trash目录下7天前的文件,同时把这些文件信息写入指定文件
判断文件是否存在
if [ -z $1 ]; then #如果没有输入参数,也就是第一个参数的字符串长度为0 : #空语句 else if [ -e $1 ]; then #如果文件存在的话 if [ -f $1 ]; then #如果文件是个普通文件? echo $1" is a text file." elif [ -d $1 ]; then #如果文件是个目录文件? echo $1" is a directory." elif [ -c $1 ]; then #如果文件是个字符设备? echo $1" is a char device." elif [ -b $1 ]; then #如果文件是个块设备? echo $1" is a block device." else #否则 echo $1" is unknow file." fi fi fi
判断文件是否存在2
#include <unistd.h> int is_file_exist(const char *file_path) { if(file_path == NULL) return -1; if(access(file_path,F_OK) == 0) return 0; return -1; }
解压文件
tar -zxvf sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz tar zxvf Download/scala-2.12.4.tgz -C MyConfigure/scala-2.12.4
执行shell脚本
在当前目录下执行./run_url.sh
如果报权限拒绝的话
chmod a+x ./run_url.sh
如何让脚本继续后台运行,哪怕关掉了终端
nohup ./xx.sh &
或者以下几种
tmux / screenxxx &! (bash / zsh)
setsid xxx还有一种工具:supervisord
@H_404_83@查询log.txt中包含keywords的上下10行
cat log.txt |grep -C 10 keywords # -A 后10行;-B 前10行
查找命令
find /home -iname "*.txt"