常用shell脚本
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
如何用脚本实现分割文件
#!/bin/bash
if [ $# -ne 2 ]; then
echo 'Usage: split file size(in bytes)'
exit
fi
file=$1
size=$2
if [ ! -f $file ]; then
echo "$file doesn't exist"
exit
fi
#TODO: test if $size is a valid integer
filesize=`/bin/ls -l $file | awk '{print $5}'`
echo filesize: $filesize
let pieces=$filesize/$size
let remain=$filesize-$pieces*$size
if [ $remain -gt 0 ]; then
let pieces=$pieces+1
fi
echo pieces: $pieces
i=0
while [ $i -lt $pieces ];
do
echo split: $file.$i:
dd if=$file of=$file.$i bs=$size count=1 skip=$i let i=$i+1
done
echo "#!/bin/bash" >; merge
echo "i=0" >;>; merge
echo "while [ $i -lt $pieces ];" >;>; merge
echo "do" >;>; merge
echo " echo merge: $file.$i" >;>; merge
echo " if [ ! -f $file.$i ]; then" >;>; merge
echo " echo merge: $file.$i missed" >;>; merge
echo " exit" >;>; merge
echo " fi" >;>; merge
echo " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >;>; merge
echo " let i=$i+1" >;>; merge
echo "done" >;>; merge
chmod u+x merge'
如何查找日期为某一天的文件
#!/bin/sh
# The right of usage, distribution and modification is here by granted by the author.
# The author deny any responsibilities and liabilities related to the code. #
OK=0
A=`find $1 -print`
if expr $3 == 1 >;/dev/null ; then M=Jan ; OK=1 ; fi
if expr $3 == 2 >;/dev/null ; then M=Feb ; OK=1 ; fi
if expr $3 == 3 >;/dev/null ; then M=Mar ; OK=1 ; fi
if expr $3 == 4 >;/dev/null ; then M=Apr ; OK=1 ; fi
if expr $3 == 5 >;/dev/null ; then M=May ; OK=1 ; fi
if expr $3 == 6 >;/dev/null ; then M=Jun ; OK=1 ; fi
if expr $3 == 7 >;/dev/null ; then M=Jul ; OK=1 ; fi
if expr $3 == 8 >;/dev/null ; then M=Aug ; OK=1 ; fi
if expr $3 == 9 >;/dev/null ; then M=Sep ; OK=1 ; fi
if expr $3 == 10 >;/dev/null ; then M=Oct ; OK=1 ; fi
if expr $3 == 11 >;/dev/null ; then M=Nov ; OK=1 ; fi
if expr $3 == 12 >;/dev/null ; then M=Dec ; OK=1 ; fi
if expr $3 == 1 >;/dev/null ; then M=Jan ; OK=1 ; fi
if expr $OK == 1 >; /dev/null ; then
ls -l --full-time $A 2>;/dev/null | grep "$M $4" | grep $2 ;
else
echo Usage: $0 path Year Month Day;
echo Example: $0 ~ 1998 6 30;
fi
如何计算当前目录下的文件数和目录数
# ls -l * |grep "^-"|wc -l ---- to count files
# ls -l * |grep "^d"|wc -l ----- to count dir
如何只列子目录?
ls -F | grep /$ 或者 alias sub = "ls -F | grep /$"(linux)
ls -l | grep "^d" 或者 ls -lL | grep "^d" (Solaris)
如何实现取出文件中特定的行内容
如果你只想看文件的前5行,可以使用head命令,
如: head -5 /etc/passwd
如果你想查看文件的后10行,可以使用tail命令,