How to write a Bash shell script - by example
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
- Fred Barclay
- 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
Oh, okay. Now I see the logic behind it.
Re: How to write a Bash shell script - by example
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.
Re: How to write a Bash shell script - by example
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.
- austin.texas
- 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
Also in the right-click context menu on the Desktop, which I find convenient...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).
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
Linux Linx 2018
Re: How to write a Bash shell script - by example
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.
Re: How to write a Bash shell script - by example
Every journey begins with a step.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.
"Shell programming is a 1950s juke box . . ." --Larry Wall
Tons of resources. Not hard to find. Shout if you want some links.
Re: How to write a Bash shell script - by example
Thanks, Wow, this stuff is really intense, I will work on learning. Can you recommend a book maybe with some examples?
Habitual wrote:Every journey begins with a step.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.
"Shell programming is a 1950s juke box . . ." --Larry Wall
Tons of resources. Not hard to find. Shout if you want some links.
Re: How to write a Bash shell script - by example
define "book" please.MurphCID wrote:Thanks, Wow, this stuff is really intense, I will work on learning. Can you recommend a book maybe with some examples?
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.
Re: How to write a Bash shell script - by example
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:define "book" please.MurphCID wrote:Thanks, Wow, this stuff is really intense, I will work on learning. Can you recommend a book maybe with some examples?
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.
-
- Level 20
- Posts: 12341
- Joined: Sun Aug 09, 2015 10:00 am
Re: How to write a Bash shell script - by example
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.
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
Regards,
Deepak
Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
- austin.texas
- 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
Scripting is very rarely a necessity. You can put it off until you chose to learn it.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?
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
Linux Linx 2018
Re: How to write a Bash shell script - by example
Thanks, I am still learning my way around the file system and command line.
austin.texas wrote:Scripting is very rarely a necessity. You can put it off until you chose to learn it.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?
- austin.texas
- 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
Try the command line links here http://www.linfo.org/how-to_index.htmlMurphCID wrote:Thanks, I am still learning my way around the file system and command line.
And 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
Linux Linx 2018
Re: How to write a Bash shell script - by example
Thank you.
austin.texas wrote:Try the command line links here http://www.linfo.org/how-to_index.htmlMurphCID wrote:Thanks, I am still learning my way around the file system and command line.
And http://caverdan.com/LinuxLinks2016.html ... ds_-_Basic
Re: How to write a Bash shell script - by example
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
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
-
- Level 4
- Posts: 443
- Joined: Fri Sep 23, 2016 10:19 am
Re: How to write a Bash shell script - by example
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.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
Re: How to write a Bash shell script - by example
Awesome Question!whitelight999 wrote:Whats the longest bash script you have written?
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.
Re: How to write a Bash shell script - by example
My longest single file script is 409 lines long; it's also my most complex one, being a multi-threaded script 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.
Re: How to write a Bash shell script - by example
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?
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?