best or most used technique when writing bash scripts...
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
best or most used technique when writing bash scripts...
hi guys,
im busy reading a good unix/linux sys admin book
on a section in bash scripts the book explains how to create scripts
but it uses this approach:
To summarize this approach:
• Develop the script (or script component) as a pipeline, one step at a time,
entirely on the command line.
• Send output to standard output and check to be sure it looks right.
• At each step, use the shell’s command history to recall pipelines and the
shell’s editing features to tweak them.
• Until the output looks right, you haven’t actually done anything, so
there’s nothing to undo if the command is incorrect.
• Once the output is correct, execute the actual commands and verify that
they worked as you intended.
• Use fc to capture your work, then clean it up and save it.
now i find this very interesting and it could take some time to get used to writing scripts this way...i have written a few and normally just use vi and then write down commands...
is it really important to write scripts using this approach,im guessing if its something rather small it could be handy...other than that use vi right?
lastly...what is the best way to test a script without having it actually execute or affect the system...do you just redirect it to a file?
Doesn't everybody do this? You don't need to redirect or use fc, just use two terminal windows. One in the regular shell, one in a text editor. Use the shell to test the commands, once you have them right, copy/paste into the text editor to build up the script. You don't need to write out the entire thing in the shell, just the key parts that need some development (greps, awks, etc.).
ok i see what you mean...but what if im using a command thats affecting files...and i want to see the results without actually altering/changing a file...is there a way to do this...or would you just backup the file?
Depends on what it's doing. If it's a rename/move you could just stick an "echo" in front of the command to print out what it's going to do without actually doing it. If it's actually modifying the file you'd need to back it up first.
You'd have the same dilemma running from a terminal as well, the command does what the command does, whether or not it's in a script.
i will follow your directions...what could i be focusing on regarding scripting to make me more valuable in the work place? i have no experience and have nowhere to turn.
I think string manipulation (including grep), variables, arrays, if statements, and for/while loops are the most important to get down. With that you can knock out most any script. There will of course be faster ways to do a lot of things, such as sed or awk one-liners, but that can come later.
ok i see what you mean...but what if im using a command thats affecting files...and i want to see the results without actually altering/changing a file...is there a way to do this...or would you just backup the file?
just echo what the results would be to the term without actually applying them
Code:
# here where the name change would take place just echo it
# instead
# mv -v "$path"/"$pref"."$ext" "$path"/"$NewFileName"
echo " mv -v "$path"/"$pref"."$ext" "$path"/"$NewFileName" "
you can echo condistionals too in order to see whats going on
Code:
echo " [[ "$var" == "7" ]] "
you can
Code:
#!/bin/bash
set -x
to watch everything that is going on within your script
what is the best way to test a script without having it actually execute or affect the system...do you just redirect it to a file?
The OP wants to know how to "dry run" a script. Depends on the script.
To correctly test a script, one must write the script is my Final Answer.
It is not clear why the OP thinks redirecting is a testing technique...
The OP wants to know how to "dry run" a script. Depends on the script.
To correctly test a script, one must write the script is my Final Answer.
It is not clear why the OP thinks redirecting is a testing technique...
more like a brain explotion lol --- I almost just ran that on / just to see .. that'd been a big oops - goood thing I changed my mind.
I do see the logic in it though - put it into a var first logic
did you even test that ever?
Quote:
To correctly test a script, one must write the script
I earned it.
I would never put "rm -fr path/to/my/doom" into a variable. And edited my original post with a stern warning.
See my earlier comment about "echo"
Quote:
Originally Posted by BW-userx
more like a brain explotion lol --- I almost just ran that on / just to see .. that'd been a big oops - goood thing I changed my mind.
I do see the logic in it though - put it into a var first logic
did you even test that ever?
Sure did.
Code:
mkdir -pv path/to/my/doom
mkdir: created directory path
mkdir: created directory path/to
mkdir: created directory path/to/my
mkdir: created directory path/to/my/doom
TRASH_THAT=$(rm -fr path/to/my/doom)
echo $TRASH_THAT
$
echo "$TRASH_THAT"
$
I earned it.
I would never put "rm -fr path/to/my/doom" into a variable. And edited my original post with a stern warning.
See my earlier comment about "echo"
Sure did.
Code:
mkdir -pv path/to/my/doom
mkdir: created directory path
mkdir: created directory path/to
mkdir: created directory path/to/my
mkdir: created directory path/to/my/doom
TRASH_THAT=$(rm -fr path/to/my/doom)
echo $TRASH_THAT
$
echo "$TRASH_THAT"
$
And the "Close the barn door after the horse has bolted testing method"
award goes to Habitual LQ Guru
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.