Question about bash scripting

ex4722

Member
Joined
Oct 14, 2020
Messages
42
Reaction score
16
Credits
344
So after a long break from linux i stated looking at scripts in bash and i got a question about it. What in the world is a script parameter? I have basic coding ideas cause i did snap coding in computer science at school but what in the world is a script parameter?
 


It's a parameter or 'argument' passed into the script at execution time. Say you have a script that does some magic disk reporting. You want to be able to use the script on any disk, so you write to take a parameter or argument which might be the disk name to be reported on. i.e. ./diskrpt.sh /dev/sda

keith
 
Consider the following command:
Bash:
ls -Alh ~/Music/
ls a command we’re all familiar with.
Everything after ls in the above command are command-line parameters to the ls executable.
NOTE: ls is a compiled binary executable, written in c and compiled to executable machine code. Not a shell-script. But the concept of command line parameters is the same for pre-compiled binaries as it is for executable scripts. They are parameters which are passed to the script/program at runtime, when the program/script is ran.

Going back to the ls example :
-Alh is a group of three parameters, telling the ls command how to format the output. (A = Almost all - lists everything except . and .., l = long list format, h = human-readable file-sizes e.g. 64k, 1Mb, 2.3Gb etc.)
And the path at the end of the command is a parameter telling ls the directory we want to list the contents of.

Sometimes parameters are mandatory, other times parameters are optional.
Sometimes programs/script require a fixed number of mandatory parameters. And sometimes they even expect them in a fixed order too!
It all depends on the script/program and the choices made by the programmers who created it.

for example, the ls command can be ran without any parameters. All parameters to it are optional and they don’t have to be specified in a fixed order either.

When writing your own scripts, adding support for command line parameters can make your scripts more flexible - allowing them to be used in multiple situations.

So for example if I was writing a script called findandmove to search /home/Jason/Downloads/ for .png files and move them to /home/jason/Pictures/png

If I chose to hard code the search path and the file extension and the output directory directly into the script - my script will only be useful for that one very specific task.

But if I set up my script to accept those things as parameters,
E.G.
findandmove {search path} {file pattern} {output path}
I’m no longer constrained to only finding png files in my Downloads directory and moving them to another directory.

Now I could run my script and tell it to search /media/jason/4Tb/ for all .mp4 files and move them to /home/jason/Videos/
Or I could search for text files in my Documents directory and move them to a backup drive.
And I could even add an optional parameter that would copy the files instead of moving them.

So now, thanks to parameters - my one script is still doing a very specific task - but it can be used in different contexts/situations, so it’s more flexible.
 
Sorry with the very late reply, I'm cramming for finals now, but i think we are talking about different script parameters. The one that im talking about is more able scripting and examples include $1 and $2. My question was about how it works in a bash script and not really the program type.
NOTE: ls is a compiled binary executable, written in c and compiled to executable machine code. Not a shell-script. But the concept of command line parameters is the same for pre-compiled binaries as it is for executable scripts. They are parameters which are passed to the script/program at runtime, when the program/script is ran.
 
Sorry with the very late reply, I'm cramming for finals now, but i think we are talking about different script parameters. The one that im talking about is more able scripting and examples include $1 and $2. My question was about how it works in a bash script and not really the program type.
What you actually asked was “what in the world are script parameters?”
And I merely explained what they are/the concept behind them.
You didn’t ask how to use them in a script, so I didn’t go into that.
If you ask the correct questions, you’ll get the answers you’re looking for!

Take a look at the link posted by @KGIII above and I’ll post later from my laptop with some practical examples!
 
If you ask the correct questions, you’ll get the answers you’re looking for!

You're not doing so, so don't worry. I'm merely adding this because it seems like a good place to add it.

I try not to take it out too hard on 'those people' (not meant to be a pejorative). They simply don't know the proper vernacular. They don't know the correct terms to even use a search engine effectively. As such, it can be extremely frustrating for them and it absolutely makes supporting them more lengthy.

That's something I need to cover in an article.
 
Cut the OP some slack, he is 15 :)

@ex4722 I am moving this to Command Line, where scripting enquiries are also handled.

@JasKinasis and @KGIII will find you there ... Jas haunts the place (chains rattle) :)

Hold on to your hats and we'll take a magic carpet ride there.

Wizard
 

Members online


Latest posts

Top