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
Fred Barclay
Level 12
Level 12
Posts: 4185
Joined: Sat Sep 13, 2014 11:12 am
Location: USA primarily

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

Post by Fred Barclay »

Oh, okay. Now I see the logic behind it. :)
Image
"Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy."
- Albert Einstein
briggsy

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

Post by briggsy »

I did a similar thing. just placed a bash script file in the templates folder and wrote a script that copies it to the /usr/bin/ directory and renames it to whatever I want then opens it in a text editor for me. saves a lot of time in the end. :)
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

You can create a directory called "bin" in your own home directory. It will be added to your path so you can run scripts that you have stored there. No need to put them in system directories.
Image
User avatar
austin.texas
Level 20
Level 20
Posts: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

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

Post by austin.texas »

xenopeek wrote: In your home directory you'll find the subdirectory called Templates. Files that you put here will become available in your file manager's Create New Document submenu that you find in the File menu and in the context menu (right-click).
Also in the right-click context menu on the Desktop, which I find convenient...
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2018
User avatar
MurphCID
Level 15
Level 15
Posts: 5895
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 »

OMG! this stuff is like ancient Tibetan to me. I have no clue what some of the stuff you are showing here is or what it does. I have so much to learn.
Habitual

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

Post by Habitual »

MurphCID wrote:OMG! this stuff is like ancient Tibetan to me. I have no clue what some of the stuff you are showing here is or what it does. I have so much to learn.
Every journey begins with a step.

"Shell programming is a 1950s juke box . . ." --Larry Wall

Tons of resources. Not hard to find. Shout if you want some links.
User avatar
MurphCID
Level 15
Level 15
Posts: 5895
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 »

Thanks, Wow, this stuff is really intense, I will work on learning. Can you recommend a book maybe with some examples?
Habitual wrote:
MurphCID wrote:OMG! this stuff is like ancient Tibetan to me. I have no clue what some of the stuff you are showing here is or what it does. I have so much to learn.
Every journey begins with a step.

"Shell programming is a 1950s juke box . . ." --Larry Wall

Tons of resources. Not hard to find. Shout if you want some links.
Habitual

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

Post by Habitual »

MurphCID wrote:Thanks, Wow, this stuff is really intense, I will work on learning. Can you recommend a book maybe with some examples?
define "book" please.

Back in 2000, I actually printed this out the Advanced Bash Scripting Guide.
Couple of hundred pages.
A Reference and Tutorial, in one.

Scripting skill comes with familiarity and most of that come just getting your hands dirty and using it every day.
User avatar
MurphCID
Level 15
Level 15
Posts: 5895
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 »

I guess the next question for me, anyway, is: Is scripting necessary to just use the system? Can I put this step off until I am a bit more comfortable with Linux actually running well?

Habitual wrote:
MurphCID wrote:Thanks, Wow, this stuff is really intense, I will work on learning. Can you recommend a book maybe with some examples?
define "book" please.

Back in 2000, I actually printed this out the Advanced Bash Scripting Guide.
Couple of hundred pages.
A Reference and Tutorial, in one.

Scripting skill comes with familiarity and most of that come just getting your hands dirty and using it every day.
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

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

Post by deepakdeshp »

Excellent introduction to scripting. How about PERL, if you are learning something new , you can try something much more powerful. I am not scripting expert but my friend has donr some wonderful things with PERL.

Perl vs Python, more knowledgeble gurus can shed some light.
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
User avatar
austin.texas
Level 20
Level 20
Posts: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

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

Post by austin.texas »

MurphCID wrote:I guess the next question for me, anyway, is: Is scripting necessary to just use the system? Can I put this step off until I am a bit more comfortable with Linux actually running well?
Scripting is very rarely a necessity. You can put it off until you chose to learn it.
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2018
User avatar
MurphCID
Level 15
Level 15
Posts: 5895
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 »

Thanks, I am still learning my way around the file system and command line.
austin.texas wrote:
MurphCID wrote:I guess the next question for me, anyway, is: Is scripting necessary to just use the system? Can I put this step off until I am a bit more comfortable with Linux actually running well?
Scripting is very rarely a necessity. You can put it off until you chose to learn it.
User avatar
austin.texas
Level 20
Level 20
Posts: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

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

Post by austin.texas »

MurphCID wrote:Thanks, I am still learning my way around the file system and command line.
Try the command line links here :arrow: http://www.linfo.org/how-to_index.html
And :arrow: http://caverdan.com/LinuxLinks2016.html ... ds_-_Basic
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2018
User avatar
MurphCID
Level 15
Level 15
Posts: 5895
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 »

Thank you.
austin.texas wrote:
MurphCID wrote:Thanks, I am still learning my way around the file system and command line.
Try the command line links here :arrow: http://www.linfo.org/how-to_index.html
And :arrow: http://caverdan.com/LinuxLinks2016.html ... ds_-_Basic
Habitual

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

Post by Habitual »

MurphCID wrote:Thank you.
De nachos.
whitelight999

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

Post by whitelight999 »

HI

question?
Fairly new to bash scripting
I had to write one for work, it kinda grew quite long as extra things got added.

Whats the longest bash script you have written?
Is there any guidelines to how long a bash script file should be before you spilt into exterior modules , i.e. multiple files
I wrote a script its now about 1500 lines in one .sh file (15 functions in it) I was told to split it up. i.e. move some of the functions into exterior modules
and have the main file load them
I don't want to I think its fine I don't want to complicate the install
i have been googling cannot really find a guideline or answer.
I see "inxi" has fifteen thousand lines of code in it one file
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

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

Post by FreedomTruth »

whitelight999 wrote: Is there any guidelines to how long a bash script file should be before you spilt into exterior modules , i.e. multiple files
I wrote a script its now about 1500 lines in one .sh file (15 functions in it) I was told to split it up. i.e. move some of the functions into exterior modules
and have the main file load them
I don't want to I think its fine I don't want to complicate the install
It's really up to the developer (or the developer's boss, whichever the case may be). Personally I think a single script should perform a single job and perform it well. I think it makes for easier maintenance (or hunting down bugs later that are not immediately apparent). If the script will be maintained or modified by others (either now or later), it may be easier for them to understand if each function is separated. if you're the only one working on it, ever, and you like it how it is, I don't see what's wrong with leaving it alone then.
Habitual

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

Post by Habitual »

whitelight999 wrote:Whats the longest bash script you have written?
Awesome Question!
Less than 100 lines?
My shell scripts are just wrappers really around someone else's work (perl/python utilities)
I tend to write quickies that are usually just one-offs of something bigger.
And I'm a firm believer in the 'do one thing, and do it well' camp. Even if it's a one-liner.
I'm happy if my scripts works at all. ;)

Have never written one close to that scope.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

My longest single file script is 409 lines long; it's also my most complex one, being a multi-threaded script :wink: Keep your files as long or as short as keeps them readable to you. After a few weeks or months not having seen your script, it will be harder to follow if it's a very long single file.
Image
whitelight999

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

Post by whitelight999 »

Optimised the code a bit. The final draft( I hope) has 1200 lines 300 of which are comments. I left it at that and refused to break it up.
I was looking at some other at some other "famous" bash programs in the repos some huge ones I mentioned inxi at 15,000 neofetch another was 3500 lines.

Where do you install the modules if you did break it up the program and remove the functions to modular files ?

usr/bin with the main script
or
/usr/lib/foo
or
someone where else?
Locked

Return to “Scripts & Bash”