2

I'm making a script that eventually starts over by executing itself again using exec. The problem is the first few lines are only supposed to be run everytime you launch the script yourself, so I'm wondering if there's any thing I can do to fix this. Here's an example:

echo "welcome"
read yesno

if [[ $something = etc.. ]]; then
whatever
fi

exec ~/bin/script

This is simplified but it this were my script I would want to ignore the first line, (echo). Is there some option to exec I can add? I mean I could make a copy of the script and just omit the first line(s), but it feels like a bad solution.

  • Why not add a parameter to the script? (Alternatively, set an environment variable which the script can test). – Thomas Dickey Nov 4 '15 at 2:17
2

With a variable, the obvious thing to do is to pass in the result from the previous prompt:

if [[ -z "$yesno" ]]; then
    echo "welcome"
    read yesno
fi

if [[ $something = etc.. ]]; then
whatever
fi

# doing it this way makes it exported into the subprocess
yesno=$yesno exec ~/bin/script
  • I knew it was something simple like that. Thank you. – DisplayName Nov 4 '15 at 2:24
-1

You can try placing a temp file in /run, check at start if it exists. If not, you can echo, if it exists, you've run it before. And when you finish remove that file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.