将shell脚本用到实处

来源:互联网 发布:人工智能利大于弊辩论 编辑:程序博客网 时间:2024/05/21 02:48

以后所有用的shell都push到github上。https://github.com/huntinux/shell


忘记bash语法,命令怎么用怎么办? man bash


1、生成有规律的下载地址。

       今天在网上搜一篇英文文章,得到一个下载地址,http://web.eecs.umich.edu/~silvio/teaching/EECS598/lectures/lecture10_3.pdf,猜想应该还有别的pdf的地址,试了试果然有几个,如lecture10_1和lecture10_2,就想着把类似的地址都生成出来,然后用wget下载,就可以实现批量下载了。


#!/bin/sh# download links http://web.eecs.umich.edu/~silvio/teaching/EECS598/lectures/*link_prefix="http://web.eecs.umich.edu/~silvio/teaching/EECS598/lectures/"for lec_num in `seq 0 1 10`dofor pdf_num in `seq 0 1 5`dowget ${link_prefix}lecture${lec_num}_${pdf_num}.pdfdonedoneexit 0


2、获得shell脚本所在目录的绝对路径

github: https://github.com/huntinux/shell/blob/master/readlink.sh

原文:http://www.zeali.net/entry/497

        要得到正在执行的程序/脚本自身所存放的绝对路径,在 PHP 里面可以用dirname(realpath(__FILE__)) ; C# 则有System.Windows.Forms.Application.StartupPath ; java 似乎没有什么比较直接的方法,只能利用 CodeSource 来间接获取 。而在 linux shell 脚本里面如果想得到当前脚本文件存放的绝对路径,也没有太现成的命令可以调用,不过可以通过下面的语句来获取:

baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)echo "full path to currently executed script is : ${baseDirForScriptSelf}"

虽说大部分情况下我们并不需要这样的绝对路径来完成工作;但如果要把多个脚本、数据文件等内容打包作为一个整体来交付别人使用,又希望不论用户拷贝到哪个目录下执行脚本都能够正确的找到这个包里面的其他内容的话,用这样的脚本来自动定位包的根目录应该是个比较鲁棒的做法。


3添加一个新的环境变量

原来自己的工作目录不够用了,所以想设置一个新的环境变量WORK, 然后执行 cd $WORK,就可以切换到新工作目录。

$vim ~/.bashrc

WROK=/run/media/huntinux/F/huntinux_work

保存退出关闭终端,再打开终端 (或者$source ~/.bashrc 这样不必重启终端

$echo $WORK

/run/media/huntinux/F/huntinux_work

$cd $WORK

成功。

(关于~/.bashrc ~/.bash_profile等的区别将这里


4、删除c源文件中未使用的变量。

       如果这样未使用的变量太多,显然手动删除太麻烦,所以想到用shell帮忙。

for i in `gcc -Wall main.c -lm 2>&1 | sed "1,2d" |tr '‘’' '  ' | cut -d' ' -f8`dosed -i '/'"$i"'/d' main.c done

其中需要把gcc的错误输出重定向到标准输出这样才可以使用管道。
接下的sed用于删除第一第二行。
tr用来将‘’替换成空格
cut用来得到未使用的变量名
接下来的循环使用sed删除包含这些变量的行,并更新文件。


5、对自己写的程序进行测试。

例如poj 1328  , 将测试数据保存到一个文件中,比如testdata,然后执行下面的脚本就不用每次都自己手动输入测试数据了。
#! /bin/sh## recompile and test the program#gcc -Wall main.c -lm && cat testdata | ./a.outexit 0

6、从daomubiji.com 下载网页,并提取出小说内容。

github: https://github.com/huntinux/daomubiji

7、flock 文件锁

github: https://github.com/huntinux/shell/blob/master/mylockfile.sh

8、逐行读文件

原文: blog.csdn.net/marcky/article/details/7549513

github: https://github.com/huntinux/shell/blob/master/readfile.sh


9、判断是否以root身份运行

# Allow only root executionif [ `id|sed -e s/uid=//g -e s/\(.*//g` -ne 0 ]; then    echo "This script requires root privileges"    exit 1fi

来源是 bitnami-gitlab 的启动脚本,ctlscript.sh。

其实就是判断当前用户的id是否为0,(root的uid为0).


id 命令的输出:

uid=1000(huntinux) gid=1000(huntinux) groups=1000(huntinux),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),125(sambashare)

这么做也是可以的:

if [ `id | cut -d' ' -f1 | cut -d'=' -f2 | cut -d'(' -f1` -ne 0 ]; then<pre name="code" class="plain">    echo "This script requires root privileges"    exit 1
fi


或者直接

id -u 获得 effective user ID

if [ `id -u` -ne 0 ]; then<pre name="code" class="plain">    echo "This script requires root privileges"    exit 1
fi







原创粉丝点击