您的位置:首页 > 运维架构 > Linux

linux 学习总结

2013-07-25 22:40 369 查看
在原来的公司,使用linux 近一年,主要是写shell脚本。换了工作之后,就没有机会使用linux了。简单的总结一下,以免忘记。
函数
使用bzip解压
decompression_bz()
{
current_path=`pwd`
dest_path=$2
if [ ! -d "$dest_path" ]
then
mkdir -p $dest_path
fi
cp -f $1*.tar  $dest_path
cd $dest_path
bzip2 -d  $1*.bz2
tar -xf $1*.tar
rm -f $1*.tar
cd $current_path
}
说明:这个方法有两个参数:第一个参数是要压缩的文件,如jre.tar.bz;第二个参数是要解压到的目的文件夹。
使用示例:
decompression_bz
jre.tar.bz /home/user2/install

sed命令就地写入
sed_i()
{
scripts="$1"
targetFile=$2
sed -e "$scripts" "$targetFile" >"$targetFile.bak"
rm -f "$targetFile"
mv "$targetFile.bak" "$targetFile"
}
示例:sed_i '/M2_HOME/'d .profile

shell 脚本
判断用户是否有home目录,
#!/bin/sh
sys_user="$1"
if grep "^$sys_user:" /etc/passwd >/dev/null  2>&1;
then
sys_home=`cat /etc/passwd |grep "^$sys_user" |awk -F : {'print $6'}`
if [ x"$sys_home" = x ]
then
echo "OS User is invalid, installer will exit."
exit 12
fi
if [ ! -d "$sys_home" ]
then
echo "OS User is invalid, installer will exit."
exit 12
fi
fi
exit 0


删除文件(通配符)
rm -f $install_dir/config/*.xml
rm -f $install_dir/config/*.conf
rm -f $install_dir/config/*.xsl
rm -f $install_dir/config/*.dtd
rm -f $install_dir/config/*.properties
rm -f $install_dir/LICENSE*
注意:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 脚本 shell bash sed