工欲善其事,必先利其器。
在软件行业,Linux做为生产力工具和Windows、IOS处于同样地位。我们要理解并掌握相关知识才能提升生产力。
《关于Unix/Linux的终端、伪终端、控制台和shell》这篇文章详细介绍了Terminal、shell(命令解析器)。Terminal是用户与电脑交互的接口,而shell则是解析用户输入的命令。Shell有很多个版本,bash、zsh、csh都源于最初的sh。本文中主要简单的介绍一点基本的bash语法。
各个版本的shell的语法在某些细节之处有着明显区别。也就是说,使用bash语法测试通过的脚本是无法确保在其它shell中使用的。
《Shell脚本学习指南》,各位看官了解一下。做为一个有时间,没能力的初学着学习这本书还是挺好的。比如当初的apexpeng。
《Shell13问》将写shell会遇到基础问题归为13个呈现给大家。
-v 一边执行脚本,一边将执行过的脚本命令打印
-n 测试脚本语法错误
-x 提供跟踪执行信息,将执行的每一条命令和结果依次打印出来。
查看终端最近输入的命令:history
查找最近输入的命令:Ctrl^R
创建目录:mkdir
当dirpath或父dirpath不存在时,自动创建依赖的目录:mkdir -p
创建新文件/更新文件时间戳:touch
查看命令的详细介绍:man,例 man touch
man文档默认的阅读工具:less
查看当前文件下的子文件:ls,ls有很多用法记得查看man文档
查看隐藏文件:ls -a
查找文件:find
删除文件:rm
移动文件:mv
拷贝文件:cp
查看文件前10行:head -10 xxxx
查看文件后10行:tail -10 xxxx
直接在terminal打印文件内容:cat xxxx
看查ubuntu版本与内核:uname -a
得到所有目录与对目录下的文件:
#! /bin/sh
[ $# -ne 1 ] &&
{
echo "usage: ./${0##*/} <path>"
exit 0
}
function notcompile ()
{
ifelf=0
#echo $1
filesinsub=$(ls)
#echo $filesinsub
for eachfile in $filesinsub
do
if [ $ifelf -eq 1 ]
then
#echo "elf file exist"
break
fi
elffileexist=$(file $eachfile | grep [Ee][lL][fF])
[ -n "$elffileexist" ] && ##if it is elf file, then run belows
{
#echo $(file $eachfile)
ifelf=1
}
done
if [ $ifelf -eq 0 ] ##有.c文件的但是没有elf文件的
then
#echo $1 ##已经可以输出了
#echo $(ls)
#echo " "
#echo " "
#echo " "
{
ismakefiles=$(ls)
for ismakefile in $ismakefiles
do
if [ "${ismakefile}" = "Makefile" ]
then
echo $1
echo $(ls)
echo " "
echo " "
echo " "
break
fi
done
}
fi
if [ $ifelf -eq 1 ]
then
ifelf=0
fi
}
dirs=$(find $1 | sort)
direxsit=0
for dir in $dirs
do
if [ -d $dir ]
then
{
cd $dir
files=$(ls)
for file in $files
do
if [ $direxsit -eq 1 ]
then
direxsit=0
break
fi
if [ "${file#*.}" = "c" ]
then
#filemeta_c=$(file $file | grep "ASCII C program text")
#if [ -n "$filemeta_c" ] ##c file exist
#then
direxsit=1
notcompile $dir
#break
fi
done
cd - > /dev/null
}
fi
done
批量修改文件中的某一个字符串(加过滤功能):
#!/bin/sh
#******************************************************************************
# Change "/bin/sh" to "/system/bin/sh"
#
#*****************************************************************************/
if [ $UID != 0 ]
then
echo "FAILED: Must have root access to execute this script"
exit 0
fi
[ $# -ne 1 ] &&
{
echo "usage: sudo ./${0##*/} <path>"
exit 0
}
echo " "
echo "grep -l -R \"/bin/sh\" $1 ..."
echo " "
files=$(grep -l -R "/bin/sh" $1 | sort)
echo "start to manipulate files: "
echo " "
for i in $files
do
{
filemeta=$(file $i | grep [eE][lL][fF])
#echo $filemeta
#echo $i
if [ -z "$filemeta" ]
then
echo $(file $i)
sed -e "s/\/bin\/sh/\/system\/bin\/sh/g" $i > ${i}_bak; mv ${i}_bak $i
chmod 777 $i
objectfile=`grep "/bin/sh" $i`
echo $objectfile
fi
}
done
shell脚本在代码中随意插入随机数据:
#!/bin/sh
######################################################################################
[ $# -ne 2 ] &&
{
echo "usage: sudo ./${0##*/} <objectpath> <englishfile>"
exit 0
}
function random()
{
min=$1;
max=$2-$1;
num=$(cat /proc/sys/kernel/random/uuid| cksum | cut -f1 -d" ");
((retnum=num%max+min));
#进行求余数运算即可
echo $retnum;
#这里通过echo 打印出来值,然后获得函数的,stdout就可以获得值
#还有一种返回,定义全价变量,然后函数改下内容,外面读取
}
#add below to the head of objectfile
sed -i "1i \/\/ ${line4}" $filename
sed -i "1a \/\/ ${line5}" $filename
#add below to the end of objectfile
sed -i '$a /*EOF*/' $filename
#add below to radomline of objectfile
filelines=$(awk 'END{print NR}' $filename)
radomline=$(random20 $filelines)
line1=$(awk '{ if(NR==(num-6)) {print $0} }' num=$randomline englishfile)
objfileradomline=$(random 20 $filelines)
echo "for line 1"
[ -z "$line1" ] ||
{
echo $line1
huanhang1=$(awk '{if(NR==testline) { if ( $NF ~/\\$/ ){ print 1 } }}' testline=${objfileradomline} $filename)
[ "1" = "$huanhang1" ] ||
{
echo "in sed 1"
sed -i "${objfileline}a \/\/ ${line1}" $filename
}
}
meld:meld官方网站
ctags:一般就搜索"F8 ctags",使用大拿搞好的vim配置文件。直接使用F8来出发tlist。
cscope(Linux下的source insight):CSCOPE官方网站
vi:《vi and Vim Editors》
git:《Pro Git》