How to write a Bash shell script - by example

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
User avatar
xenopeek
Level 25
Level 25
Posts: 29459
Joined: Wed Jul 06, 2011 3:58 am

Re: How to write a Bash shell script - by example

Post by xenopeek »

The script the user invokes should be in /usr/bin. The other scripts should go to /usr/lib/foo. Non-executable files might instead go to /usr/share/foo. You can look at Linux Mint's programs like Update Manager that are Python scripts, where they store which kind of files: https://github.com/linuxmint/mintupdate/tree/master/usr
Image
Lemongrass38

Re: How to write a Bash shell script - by example

Post by Lemongrass38 »

Habitual wrote:

Code: Select all

#!/bin/bash
# Purpose:	Inserts #!/bin/bash into a file and makes it executable
# Usage: 	scriptname /path/to/file.sh
# Author: 	JJ/Habitual
# Date: 	Tue Jul 19, 2011
# Version: 	1311133977
# Disclaimer:	Use it, abuse it, just don't lose it.
MINPARAMS=1

if [ -n "$1" ]
then
echo "#!/bin/bash" > $1
chmod 700 "$1"
echo File $1 is now an executable and ready to be edited.
fi 

if [ $# -lt "$MINPARAMS" ]
then
  echo Usage: `pwd`/`basename $0` /path/to/script.sh
fi  
exit 0
...
Enjoy! Now let's code something!!!
Thank you for that, Habitual! ;) Because of this encouragement and example, I've just created my first useful and working bash script :D:

Code: Select all

#!/bin/bash
# Purpose    :   Updates the user.js folder in the firefox profile directory
# Usage      :   Close Firefox, then run script ./firefox.user.js.sh Firefox_profile_name. If no argument is added, it will use the default profile.
# Author     :   Lemongrass
# Date       :   2017.01.14.
# Version    :   0.1.
# Disclaimer :   Only for GNU/Linux. Use it at your own risk.

MAXPARAMS=1

if [ $# -gt "$MAXPARAMS" ]

then
echo "Too many parameters. Usage: close Firefox, then run script ./firefox.user.js.sh Firefox_profile_name. If no argument is added, the default profile will be used."
exit 0

fi

if [ -n "$1" ]

then
rm -rf ~/.mozilla/firefox/????????.$1/user.js
git clone https://github.com/pyllyukko/user.js
mv  user.js ~/.mozilla/firefox/????????.$1

else
rm -rf ~/.mozilla/firefox/????????.default/user.js
git clone https://github.com/pyllyukko/user.js
mv  user.js ~/.mozilla/firefox/????????.default

fi


exit 0

#EOF
Habitual

Re: How to write a Bash shell script - by example

Post by Habitual »

Lemongrass38 wrote:Thank you for that, Habitual! ;) Because of this encouragement and example, I've just created my first useful and working bash script :D:
Thanks to all who contribute{d} to the end result.
I'm pretty certain I stole it off the 'net and ran it through my Linux Chop Shop.
User avatar
MurphCID
Level 15
Level 15
Posts: 5890
Joined: Fri Sep 25, 2015 10:29 pm
Location: Near San Antonio, Texas

Re: How to write a Bash shell script - by example

Post by MurphCID »

Still a great thread for us newbies, so I am going to bump it back up.
Locked

Return to “Scripts & Bash”