Skip to content

Instantly share code, notes, and snippets.

@photonxp
Last active October 7, 2018 10:56
Show Gist options
  • Save photonxp/056d3526d46d850722842b3ec2cd0e85 to your computer and use it in GitHub Desktop.
Save photonxp/056d3526d46d850722842b3ec2cd0e85 to your computer and use it in GitHub Desktop.
touch sh bash files with additional operations such as give file names and chmod them
#!/bin/sh
# Once the global warmth reaches a tipping point,
# the chain-reaction could be irreversible,
# so are we ready for the ticking bomb?
# Usage:
# ./touchsh file_name
# No touch operation if file already existed.
n_touch(){
fpath=$1
printf "$fpath" | grep -Eq '\.bash$'
if [ 0 -eq $? ]
then
echo "NOTICE:: Found name suffix .(ba)sh in input"
fpath_new=$fpath
else
fpath_new=$fpath.bash
fi
if [ -e $fpath_new ]
then
echo "NOTICE:: $fpath_new"
echo "NOTICE:: File already existed. Exit with no operation."
return 1
fi
touch $fpath_new
printf "#!/bin/bash\n\n" > $fpath_new
chmod u+x $fpath_new
echo $fpath_new
}
fpaths_str="$@"
for fpath in $fpaths_str
do
n_touch $fpath
done
#!/bin/sh
# Usage:
# ./touchsh file_name
# No touch operation if file already existed.
n_touch(){
fpath=$1
printf "$fpath" | grep -Eq '\.sh$'
if [ 0 -eq $? ]
then
echo "NOTICE:: Found name suffix .(ba)sh in input"
fpath_new=$fpath
else
fpath_new=$fpath.sh
fi
if [ -e $fpath_new ]
then
echo "NOTICE:: $fpath_new"
echo "NOTICE:: File already existed. Exit with no operation."
return 1
fi
touch $fpath_new
printf "#!/bin/sh\n\n" > $fpath_new
chmod u+x $fpath_new
echo $fpath_new
}
fpaths_str="$@"
for fpath in $fpaths_str
do
n_touch $fpath
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment