[原创] Linux Shell 常用代码片断
持续更新中......
序列生成及数字填充
$ for i in $(seq -f "%03g" 1 5);do echo $i;done
001
002
003
004
005
拷贝文件时忽略指定的目录
rsync -av --progress sourcefolder /destinationfolder --exclude="thefoldertoexclude"
说明:thefoldertoexclude 是相对于 sourcefolder 目录的。也就是说忽略 sourcefolder 目录中的 thefoldertoexclude 目录
将当前目录下的所有文件的文件名变成小写字母
$ for i in `find .`;do mv $i `echo $i |tr [a-z] [a-z]`;done
后台运行程序,将结果输出到 /dev/null 中,并将标准出错重定向到标准输出
nohup java -jar you-java.jar > /dev/null 2>&1 &
克隆网站到本地目录,可实现本地访问 https://askubuntu.com/a/512897
wget --mirror -p --convert-links -P ./LOCAL-DIR WEBSITE-URL
Options: --mirror turns on options suitable for mirroring. -p downloads all files that are necessary to properly display a given HTML page. --convert-links after the download, convert the links in document for local viewing. -P ./LOCAL-DIR saves all the files and directories to the specified directory.
获取 Shell 脚本的第一个参数和最后一个参数
#!/bin/bash
numArgs="$#"
echo "Number of args: $numArgs"
firstArg="$1"
echo "First arg: $firstArg"
lastArg="${!#}"
echo "Last arg: $lastArg"
allArgs="$@"
echo "All arguments: $allArgs"
allArgsExeptLastOne="${@:1:$(($#-1))}"
echo "All arguments except last one: $allArgsExeptLastOne"
测试结果如下:
$ ./test.sh a b c d e f
Number of args: 6
First arg: a
Last arg: f
All arguments: a b c d e f
All arguments except last one: a b c d e