How to write a Bash shell script - by example

Forum rules
Before you post please read how to get help
User avatar
xenopeek
Level 24
Level 24
Posts: 22476
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Post by xenopeek » Sat Oct 15, 2016 2:33 pm

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
Level 4
Level 4
Posts: 344
Joined: Mon Dec 12, 2016 11:29 pm
Location: Central Europe

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

Post by Lemongrass38 » Sat Jan 14, 2017 5:43 pm

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
If your issue is solved, please be so kind and indicate that by editing the topic title in the first post. :)

Habitual
Level 13
Level 13
Posts: 4871
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

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

Post by Habitual » Tue Jan 24, 2017 8:51 am

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.

Post Reply

Return to “Scripts & Bash”