3
\$\begingroup\$

I created this bash script to make it easy to publish your files to a GitHub repository.

#!/bin/bash

echo "Welcome to the GitHub File Publishing Tool (GHFP for short)"
read -p "Continue? " yn

case $yn in
[Yy]* ) echo "Continuing the program...";;
[Nn]* ) echo "Stopping the program"; exit;;
esac

echo "Your remote will be shown below."

git remote

read -p "What is the remote of your repository? See above for the remote. " REMOTE

echo "Your main branch will be shown below."

git remote show $REMOTE | sed -n '/HEAD branch/s/.*: //p'

read -p "What branch would you like to write changes to? For example, enter master for main/default branch. " BRANCH

echo "The number of commits for branch" $BRANCH "will show below."

git rev-list --count $BRANCH

git add .

read -r -p "Commit name/number/identifier: " NUM


git commit -m $NUM

git push

Can anyone provide some ideas for improvement?

Below is the output of the file (running for a repository with no new changes)

bash gitpush.sh
Welcome to the GitHub File Publishing Tool (GHFP for short)
Continue? y
Continuing the program...
Your remote will be shown below.
upstream
What is the remote of your repository? See above for the remote. upstream
Your main branch will be shown below.
master
What branch would you like to write changes to? For example, enter master for main/default branch. master
The number of commits for branch master will show below.
18
Commit name/number/identifier: 19
On branch master
Your branch is up to date with 'upstream/master'.

nothing to commit, working tree clean
Everything up-to-date
New contributor
Logan Alldredge is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
  • \$\begingroup\$ Add a default in your case, take into account that the user could press any key besides y an n. \$\endgroup\$ 4 hours ago

1 Answer 1

4
\$\begingroup\$

Rather than make it interactive

read -p "Continue? " yn

I would make this more like other command line tools. You have default values and allow specialization via flags (or error out if required parameters are not provided).

The point of scripts is to automate an action. If your script is interactive then it does not help in automation.

Now you can have a script that collects information interactively. But then calls you script proving all the parameters.

Checking for errors in your commands is also a good idea. So that you can exit out. Couple of ways to do that:

// Change the first line to this.
// Forces the script to exit if there are any errors.
#!/bin/bash -e

Or

// Use the "||" which executes the second  command if the
// first command does not succeed.
git remote || echo "Fail";exit 1
\$\endgroup\$
3
  • \$\begingroup\$ You are saying that, in order to make it automatic, one would have to not include user prompts, correct? \$\endgroup\$ 6 hours ago
  • 2
    \$\begingroup\$ You could have an option to ask for user input. But by default you should provide all the parameters on the command line to the script. \$\endgroup\$ 6 hours ago
  • \$\begingroup\$ Also, I have not really done a whole lot with bash scripts... Do you know how to set a default variable? \$\endgroup\$ 6 hours ago

Your Answer

Logan Alldredge is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.