sh 将sh文件转换为bash,反之亦然

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 将sh文件转换为bash,反之亦然相关的知识,希望对你有一定的参考价值。

#!/bin/bash

# Once the global warmth reaches a tipping point,
#     the chain-reaction could be irreversible,
#     so are we ready for the ticking bomb?

# Usage:
# ./bash2sh.bash ./test/b.bash c.bash [...]
# ./bash2sh.bash "  " ./test/a ./test/{b,c}.bash ./test/dir/{.,d.bash}
# 
# ####./test_bash2sh.bash:
# #!/bin/bash
#
# if [[ -e ./test9 && "-o" != "$1" ]]
#     then
#     echo "Dir test9 existed. Quit."
#     exit 1
# fi
#
# rm -r ./test9
# mkdir -p ./test9/dir
# touch ./test9/a ./test9/dir/d.bash
# echo "#!/bin/bash" > ./test9/b.bash
#
# ./bash2sh.bash "  " ./test9/a ./test9/{b,c}.bash ./test9/dir/{.,d.bash}
# 

sed_pattern_shebang_bash2sh='1s/(^#.*)ba(sh$)/\1\2/'
arr_cmd_sed=(sed -r "$sed_pattern_shebang_bash2sh")

notice_with_errmsg(){
    errmsg=$1
    echo "NOTICE:: $f1path"
    echo "NOTICE:: $errmsg Leave file with no operation."
}

check_f1path(){
    if [ ! -f $f1path ]
        then
        notice_with_errmsg "Not a regular file."
        return 1
    fi
    
    if [ ! -w $f1path ]
        then
        notice_with_errmsg "Not a writable file."  
        return 1
    fi
    
    printf "$f1path" | grep -Eq '\.bash$'
    if [ 0 -ne $? ] 
        then
        notice_with_errmsg "Not a .bash file."
        return 1
    fi
}

sed_file(){
    # echo "f1path: $f1path"
    "${arr_cmd_sed[@]}" -i $f1path
    # cat $f1path
}

make_f2path(){
    f2path="${f1path%.bash}.sh"
}

mv_file(){
    make_f2path
    mv_cmd="mv $f1path $f2path"
    echo $mv_cmd
    $mv_cmd
}

do_file(){
    do_steps="check_f1path sed_file mv_file"
    for do_step in `echo $do_steps` 
        do
        $do_step
        if [ 0 -ne $? ]
            then
            echo "NOTICE:: Task on the file failed."
            return 1
        fi
    done
}

main(){
    path_arglist=$@
    for f1path in $path_arglist
        do
        do_file
    done
}

main $@
#!/bin/bash

# Once the global warmth reaches a tipping point,
#     the chain-reaction could be irreversible,
#     so are we ready for the ticking bomb?

# Usage:
# ./sh2bash.bash ./test/b.sh c.sh [...]
# ./sh2bash.bash "  " ./test/a ./test/{b,c}.sh ./test/dir/{.,d.sh}
# 
# ####./sh2bash.bash:
# #!/bin/bash
#
# if [[ -e ./test9 && "-o" != "$1" ]]
#     then
#     echo "Dir test9 existed. Quit."
#     exit 1
# fi
#
# rm -r ./test9
# mkdir -p ./test9/dir
# touch ./test9/a ./test9/dir/d.sh
# echo "#!/bin/sh" > ./test9/b.sh
#
# ./sh2bash.bash "  " ./test9/a ./test9/{b,c}.sh ./test9/dir/{.,d.sh}
# 

sed_pattern_shebang_sh2bash='1s/(^#.*)(sh$)/\1ba\2/'
arr_cmd_sed=(sed -r "$sed_pattern_shebang_sh2bash")

notice_with_errmsg(){
    errmsg=$1
    echo "NOTICE:: $f1path"
    echo "NOTICE:: $errmsg Leave file with no operation."
}

check_f1path(){
    if [ ! -f $f1path ]
        then
        notice_with_errmsg "Not a regular file."
        return 1
    fi
    
    if [ ! -w $f1path ]
        then
        notice_with_errmsg "Not a writable file."
        return 1  
    fi

    printf "$f1path" | grep -Eq '\.sh$'
    if [ 0 -ne $? ] 
        then
        notice_with_errmsg "Not a .sh file."
        return 1
    fi
}

sed_file(){
    # echo "f1path: $f1path"
    "${arr_cmd_sed[@]}" -i $f1path
    # cat $f1path
}

make_f2path(){
    f2path="${f1path%.sh}.bash"
}

mv_file(){
    make_f2path
    mv_cmd="mv $f1path $f2path"
    echo $mv_cmd
    $mv_cmd
}

do_file(){
    do_steps="check_f1path sed_file mv_file"
    for do_step in `echo $do_steps` 
        do
        $do_step
        if [ 0 -ne $? ]
            then
            echo "NOTICE:: Task on the file failed."
            return 1
        fi
    done
}

main(){
    path_arglist=$@
    for f1path in $path_arglist
        do
        do_file
    done
}

main $@

以上是关于sh 将sh文件转换为bash,反之亦然的主要内容,如果未能解决你的问题,请参考以下文章

sh Bash脚本使用Applescript将目录中的所有html文件转换为docx文件。它还将标题和普通字体转换为

sh 将Bash变量转换为大写

sh Bash:echo:obase,ibase,bc:将二进制转换为十进制,将十进制转换为二进制

sh 将INI文件解析为Bash字典

sh bash中的IP转换为Integer和Integer转换为IP转换。

sh Bash脚本批量转换.MKV文件到.MP4与avconv