linux 学习总结

在原来的公司,使用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*

注意:

rm-f "$install_dir/config/*.xml" 是错误的,因为含有通配符。



判断目录是否可写:

test -w "/home/whuang/"

判断目录是否可读:

test -r "/home/whuang/"



你可能感兴趣的:(linux,shell,脚本,bash,sed)