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.
I have a problem as mentioned below. Would like to know for some suitable fix for this.
In my directory structure (with some test scripts in that directory), as soon you enter some directory, one is supposed to run some pre-run script and then run the test scripts in that directory.
When they move to another directory, similarly they have to run respective pre-run script of that directory.
My problem is, people are running the pre-run script , running their scripts in that directory and are supposed to run post-run script of that directory (Which basically reset the flags set in pre-run).
But people are just moving to another directory without running post-run script. How can I tackle this problem.
Is there any lock mechanism in Linux, which basically locks the running of pre-run scripts back-to-back.
Thanks colucix for your suggestion. Even I thought of this overloading of cd at first instance, but this may irk other users of this system.
Thanks john test for your suggestion. But this may not suffice, as because I would not know which post-run script to run, if i just prepend post-run to pre-run. Because, user may come to this directory from any directory, And I need only respective directories post run to be run.
I was thinking of some lock in /tmp directory, Which I can check in every pre-run file and if not present then, I would proceed running pre-run or else, get the name of the lock (which I will create like pre-run-<directory name>, run the post-run of this directory name, then create lock for this pre-run and go on like that ...
Any improved suggestions ...
Thanks Wim Sturkenboom for your suggestion, but user may wish to run only a single script in the directory .
If I write a script to run pre-run, run scripts, post-run script, then he is always forced to run all scripts in the directory.
I want to give the user freedom to run his intended script.
But for this he had to run pre-run script, run his intended script and mandatorily run post-run script.
The users are running pre-run script to make their intended script. But they are not running post-run script (Which is not making the flags unset which were set in pre-run script.)
What you want to accomplish is usually handled by "flags." In you case, it would be simple to create a "per-user" directory in /tmp and do a touch /tmp/<user>/<directory name> when run the "start" script, and a rm -f /tmp/<user>/<directory name> when they run the "exit" script.
All the "start" scripts could check for the existence of any "unclosed" directories for the user.
The "test" scripts could also, individually, check that the "start" script had been run (and run it) before they run.
Basically, just use the existence of some (zero length) file to flag various states.
Here's some (untested) code:
Code:
#!/bin/bash
function at_start()
{
here=$(basename pwd)
[ -d "/tmp/locks/$USER" ] || mkdir "/tmp/locks/$USER"
if [ -e "/tmp/locks/$USER/$here" ]; then
echo Already started in this ("$here") directory.
else
touch "/tmp/locks/$USER/$here"
fi
for dir in "/tmp/locks/$USER/*";do
[ "$dir" = "$here" ] && continue
read -n 1 -p "You have not finished directory $dir. Do you want to do that now? (Y/n)" ans
case "$ans" in
n|N) break;;
*) ../$dir/finalization;;
esac
done
}
function while_running()
{
here=$(basename pwd)
[ -e "/tmp/locks/$USER" ] || ./initialization
}
function at_end()
{
here=$(baseneme pwd)
if [ -e "/tmp/locks/$USER/$here ]
then
rm -f "/tmp/locks/$USER/$here ]
else
echo You can't run the finalization script unless the initialization script has been run.
fi
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.