脚本内容如下:

#!/bin/bash

####################################################################
# Author:                netcat
# Lable:                 passwd.sh
# Information:           Set password for script
# Create Date:           2011-02-12
# Version:               v1.0
####################################################################
stty -echo
echo -n "Please input your password: "
read pass1
echo ""
echo -n "Please input your password again: "
read pass2
echo ""
stty echo
if [ "$pass1" != "$pass2" ]; then
    echo "Two passwords is not same"
else
        if [ -z "$pass1" ];then
        echo "password is blank"
        else
        cat $1|grep "#!" >/dev/null
                if [ $? -ne 0 ];then
                 echo "#!/bin/bash" > hh
                 echo "pass=$pass1" >> hh
                 echo "stty -echo" >>hh
                 echo "read -p \"Please input password: \" ps" >>hh
                 echo "echo \" \" " >>hh
                 echo "stty echo" >>hh
                 echo "if [ \$ps != \$pass ]" >>hh
                 echo "then">>hh
                 echo "echo \"your password is wrong\" ">>hh
                 echo "else" >>hh
                 cat $1 >>hh
                 echo "fi">>hh
                 cat hh > $1
                 rm -f hh
                 echo "$1 has been encryptioned"
                else
                 echo "#!/bin/bash" >hh
                 echo "pass=$pass1" >> hh
                 echo "stty -echo">>hh
                 echo "read -p \"Please input password: \" ps" >>hh
                 echo "echo \" \" ">>hh
                 echo "stty echo">>hh
                 echo "if [ \$ps != \$pass ]" >>hh
                 echo "then">>hh
                 echo "echo \"your password is wrong\" ">>hh
                 echo "else" >>hh
                 cat $1 |grep -v "\#\!/bin/bash" >>hh
                 echo "fi">>hh
                 cat hh > $1
                 rm -f hh
                 echo "$1 has been encryptioned"
fi
fi
fi

使用方法:

加密前:

给shell脚本加密码的shell脚本_shell脚本

加密后:

密码错误

 给shell脚本加密码的shell脚本_休闲_02 

密码正确

给shell脚本加密码的shell脚本_职场_03

脚本做的比较粗糙,很多方面都不完善,而且存在很多漏洞。

本人正在不断完善中,如果有兴趣的朋友,可以加我的qq:297020555,或发邮件给我:houwenhui1990@126.com