Skip to content

Instantly share code, notes, and snippets.

@photonxp
Last active October 7, 2018 11:04
Show Gist options
  • Save photonxp/63868182f4fe397fb0e6cda72e5baac4 to your computer and use it in GitHub Desktop.
Save photonxp/63868182f4fe397fb0e6cda72e5baac4 to your computer and use it in GitHub Desktop.
convert sh file to bash or vice versa
#!/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 $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment