How to write a Bash shell script - by example

Forum rules
Before you post please read how to get help
User avatar
Fred Barclay
Level 12
Level 12
Posts: 4141
Joined: Sat Sep 13, 2014 11:12 am
Location: Bumping around in the bush

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

Post by Fred Barclay » Sun Dec 20, 2015 4:54 pm

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
Level 1
Level 1
Posts: 1
Joined: Wed Jan 21, 2015 2:24 am

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

Post by briggsy » Mon Dec 28, 2015 4:39 am

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 24
Level 24
Posts: 22637
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Post by xenopeek » Mon Dec 28, 2015 5:02 am

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: 12055
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 » Fri May 27, 2016 7:44 am

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 5
Level 5
Posts: 524
Joined: Fri Sep 25, 2015 10:29 pm

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

Post by MurphCID » Wed Sep 07, 2016 10:19 pm

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
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 » Fri Sep 09, 2016 12:31 pm

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 5
Level 5
Posts: 524
Joined: Fri Sep 25, 2015 10:29 pm

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

Post by MurphCID » Fri Sep 09, 2016 3:14 pm

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
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 » Sat Sep 10, 2016 8:16 am

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 5
Level 5
Posts: 524
Joined: Fri Sep 25, 2015 10:29 pm

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

Post by MurphCID » Sat Sep 10, 2016 6:48 pm

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 11
Level 11
Posts: 3515
Joined: Sun Aug 09, 2015 10:00 am

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

Post by deepakdeshp » Sat Sep 10, 2016 7:25 pm

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, and keeps the forum clean.
I am using Mint 19 Cinnamon 64 bit with AMD A8/7410 processor . Memory 8GB

User avatar
austin.texas
Level 20
Level 20
Posts: 12055
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 » Sat Sep 10, 2016 7:25 pm

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 5
Level 5
Posts: 524
Joined: Fri Sep 25, 2015 10:29 pm

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

Post by MurphCID » Sun Sep 11, 2016 10:58 am

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: 12055
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 » Sun Sep 11, 2016 11:07 am

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 5
Level 5
Posts: 524
Joined: Fri Sep 25, 2015 10:29 pm

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

Post by MurphCID » Sun Sep 18, 2016 5:31 pm

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
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 » Sun Sep 18, 2016 6:47 pm

MurphCID wrote:Thank you.
De nachos.

whitelight999
Level 1
Level 1
Posts: 16
Joined: Sun Feb 28, 2016 3:56 pm

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

Post by whitelight999 » Sat Oct 08, 2016 12:53 pm

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: 412
Joined: Fri Sep 23, 2016 10:19 am

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

Post by FreedomTruth » Fri Oct 14, 2016 7:37 pm

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.
If you abide in My word, you are My disciples indeed. And you shall know the truth, and the truth shall make you free... Most assuredly, I say to you, whoever commits sin is a slave of sin... Therefore if the Son makes you free, you shall be free indeed.

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 » Sat Oct 15, 2016 9:26 am

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 24
Level 24
Posts: 22637
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 11:39 am

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
Level 1
Level 1
Posts: 16
Joined: Sun Feb 28, 2016 3:56 pm

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

Post by whitelight999 » Sat Oct 15, 2016 2:18 pm

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?

Post Reply

Return to “Scripts & Bash”