How do I make these shell scripts with ash as my shell?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
How do I make these shell scripts with ash as my shell?
Hello
I want to make two shell scripts but they shell scripts isnt exactly my language :P
The following is all pseudocode
The first script will run every say 5 minutes:
Code:
if /tmp/media/sda1 exists or /tmp/media/sda2 exists or /tmp/media/sda3 exists or /tmp/media/sda4 exists
then do
variable storage=what ever above first returns true (lets say /tmp/media/sda3)
//storage=/tmp/media/sda3
if storage+"/"+.runmescript exist
then do
./runmescript
end if
end if
thats the first one.
Now we have on the harddrive a file called "someconf" at /etc/ also at /tmp/media/sda3 we have a file called "update". someconf has a line that says "dot=99" while update has a line that says "dot=11"
".runmescript" would be.
Code:
load /etc/someconf
search for "dot=99" in /etc/someconf
load /tmp/media/sda3/update
search for "dot=11"
replace line from update in somesonf
save
#! /bin/ash
volume=$(ls /tmp/media | grep sda[1|2|3|4])
if [ -n $volume ]
then do blah blah blah
And then I get stuck on the +"/"+runmescript. ??
Can I ask exactly what you're trying to do? Please be specific.
Crontab will be running this script every x minutes.
If it detects sda1/sda2/etc, it will check if on that volume "runmescript" exists. If it exists, it will run it.
runmescript will scan the local hdd for a file named "conf". If it exists (it will always exist but just in case this check is neccesary) Inside "conf" there will be a line similar to "number=99". runmescript will have a variable named "$numberreplacement=11". When runmescript is ran, it will overwrite the value in conf of "number=99" to "number=11" and save the file (conf). Once it is ran, to avoid further writing and overwriting the same thing over and over, the script will delete itself and/or will unmount the usb device from which it ran from.
Please if anymore doubts, ask Thanks for the help.
If you want to write a shell script, find yourself a good scripting tutorial or two and start working at it. Don't expect us to do all the work for you.
Here are a few useful bash scripting references, I recommend starting with the first link, or perhaps linuxcommand.
However, in order to write your scripts specifically for ash, you'll have to limit the contents to only syntax that it can understand, that is, avoid using anything outside of the POSIX standard.
This link has a good breakdown of commands that are specific to bash, and their POSIX equivalents.
#! /bin/ash
volm=$(ls /tmp/media | grep "sda[1|2|3|4]" | head -n1)
if [ -n $volm ] ; then
if [ -a /tmp/media/$volm/runmescript ]; then
/tmp/media/$volm/runmescript ;
else
echo "runmescript does not exist!"
fi
else
echo "The target volume does not exist!" ;
fi
I tested this with bash, so test first to make sure it works with ash.
Quote:
Now we have on the harddrive a file called "someconf" at /etc/ also at /tmp/media/sda3
This file is always on sda3? In that case your current script (which looks for sda1,2,3 or 4) will not work if sda1 and 2 are not present. Please clarify. Or better yet, write it yourself. Since now you've gotten somewhat of an idea.
Why is the head useless? If 2 or more of the directories exist you need to select only a single one, no?
I should replace "useless" with inefficient: you don't need to fire up external binaries until you've exhausted what your shell provides you with. And even then you can prefer to use builtins like "break".
Quote:
Originally Posted by ntubski
/tmp/media/sda[1234]
Sure it's "[]" and not "{}"? Here's "eval echo /tmp/media/sda{1..4}" in a few shells BTW:
I should replace "useless" with inefficient: you don't need to fire up external binaries until you've exhausted what your shell provides you with. And even then you can prefer to use builtins like "break".
Meh, if you want efficient don't use a shell script.
Quote:
Sure it's "[]" and not "{}"?
Yes, "{}" is brace expansion which isn't specified by POSIX; "[]" is globbing which is.
Quote:
Here's "eval echo /tmp/media/sda{1..4}" in a few shells BTW:
What's that? A useless use of eval?
Here's "echo /tmp/media/sda[1234]" in a few shells:
Why is the head useless? If 2 or more of the directories exist you need to select only a single one, no?
That's exactly why I included it; so that the variable wouldn't be "sda1 \n sda2 \n sda3" or something to that effect...
I make no pretenses at being proficient in scripting; I just like the challenge, and a little experimentation! I was hoping someone could help show better ways to do it.
Notice that there's a difference between brace expansion and pattern-matching globbing anyway.
Brace expansion generates every possible combination first, then runs the given operation using that list. This means you'll get "file not found" errors on every combo that doesn't exist, assuming you use it in that fashion. But it is very handy for generating lists of similar text strings, particularly in loops, with printf, or in commands like mkdir.
Pattern matching, OTOH, simply searches for any existing files that fit the pattern and returns them as a list. Only if nothing is found at all do you possibly get an error, as it will then return the literal globbing string (the exact behavior may be configurable with shell settings). globbing is also used to test arbitrary text strings in case statements and the expanded [[..]] test facility in bash/ksh.
evalis occasionally used with brace expansion because of the shell parsing order. Since the shell processes brace expansions before doing parameter expansions, a pattern like {$a..$b} will not work without it.
I would not generally recommend this, however, due to eval's security issues. Unless you can guarantee in your code that the variables will always be clean, you should use other techniques instead, such as seq.
Finally, a note on the results of unSpawn's brace expansion test. His first one shows "sh" as expanding them normally, but it actually depends entirely on which shell your system has been set up with to process /bin/sh. Since brace expansion is simply undefined by posix, not counter to it, it's left up to the shell doing the executing to decide how to handle such code. unSpawn's system obviously has /bin/sh linked to a shell that understands brace expansion, probably bash. Most POSIX shells will simply treat them as literal text strings, as the rest of his tests demonstrate.
Note also that csh and tcsh are not bourne-based shells, and not posix-compliant, so it's no surprise that they give different results. Their scripting syntax has many differences compared to the common Linux shells (and their behavior is less predictable).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.