-
-
Save wesscoby/28963b2458413e6bfc7ea4c417de82ff to your computer and use it in GitHub Desktop.
Creates a new bash script file in the current working directory. This is meant for #WSL users as it kind of solves the CR [Carriage Return (\r)] and LF [Linefeed (\n)] issue in Windows, since the file will be created in the Linux environment, and not Windows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Title : csf | |
# Date : 17.05.2019 | |
# Author : WessCoby <[email protected]> | |
# Version : 1.0.0 | |
# Description : Short for 'Create Script File' [A simple bash script creator] | |
# Creates a new bash script file in the current working directory. | |
# This kind of solves the CR [Carriage Return (\r)] and LF [Linefeed (\n)] issue in Windows, | |
# since the file will be created in a Unix environment, and not Windows. | |
# Therefore, after creating the script file, just open it in your editor and start writing your script | |
# Options : Specify file name [you can choose to add a bash extension] | |
#--------------------------------------------------------------------- | |
# FUNCTIONS | |
#--------------------------------------------------------------------- | |
### Prompt for file name | |
prompt() { | |
read -r -p "File name(s) [Space or comma separated]: " file; | |
echo "$file"; | |
} | |
### Create a script file with argument as name, | |
### set execute permissions | |
### and add brief documentation | |
createScriptFile() { | |
if [[ ! -e "$PWD/$1" ]]; | |
then | |
touch "$PWD/$1" # Create file | |
chmod u+x ${1} # Modify the file's permission | |
# Adding some content to the newly created file | |
printf "%s\n\n" "#!/bin/bash" > ${1} # shebang! | |
# A brief documentation | |
printf "#: Title: %s\n" "$1" | cat >> $1 | |
printf "#: Date: %s\n" "$(date)" | cat >> $1 | |
printf "#: Author: %s\n" "[Your name]" | cat >> $1 | |
printf "#: Version: %s\n" "1.0.0" | cat >> $1 | |
printf "#: Description: %s\n\n" "[A brief description of your script's functionality]" | cat >> $1 | |
printf "# Begin Scripting " | cat >> $1 | |
fi | |
} | |
#--------------------------------------------------------------------- | |
# START | |
#--------------------------------------------------------------------- | |
### If no argument is passed when script is invoked | |
if [[ $# == 0 ]]; | |
then | |
# Prompt for file name(s) and save input in array (names) | |
IFS=', ' read -r -a names <<< $(prompt) | |
for name in "${names[@]}" | |
do | |
createScriptFile ${name}; | |
done | |
exit 0; | |
elif [[ $# == 1 ]]; # if script is invoked with 1 argument | |
then | |
createScriptFile $1 | |
exit 0; | |
else # if 2 or more arguments | |
IFS=', ' read -r -a filenames <<< $@ | |
for file in "${filenames[@]}" | |
do | |
createScriptFile "$file"; | |
done | |
exit 0; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE
[ ~/bin ]
, if you don't already have it and export it to your PATH[ ~/bin/csf ]
chmod u+x csf
csf filename
creates a single script filecsf filename1 filename2 filename3
creates multiple files in the current working directorycsf
will prompt user to enter file name(s).