LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A stupid "while true" loop question (https://www.linuxquestions.org/questions/programming-9/a-stupid-while-true-loop-question-4175712394/)

ortizimo 05-20-2022 09:59 AM

A stupid "while true" loop question
 
Please excuse my ignorance...I only do what I need and move on.

I've been trying to clear a variable to initiate another go at the process and cannot make it work. I've Frankenstein this below:

while true; do
read -p "Do you want to scan another file? (y\n) " yn
case $yn in
[Yy]* ) unset ${TARGET_FILE}
ls -h
read -p "Enter the name of the file: " file
read $file >> ${TARGET_FILE}
exec ./runpe ${TARGET_FILE}; break;;
[Nn]* ) break;;
esac
done

Basically, I've created a bash script to use various CLI tools to scan files for forensic analysis. This portion above would "allow" me to restart the entire script or at least that's what I need it to do.

I have a co-worker that says I have to either wrap the entire code in a while loop or copy and paste the entire thing all over again for the next round of files. This doesnt sound correct to me and he doesnt want to help me out.

Thank you all in advance.

jailbait 05-20-2022 10:17 AM

Quote:

Originally Posted by ortizimo (Post 6355121)

while true; do

This statement makes no sense. If true is a variable you do not initialize it and you do not change its value in the loop. Or if you mean while (some condition) is true then you need to put in the (condition) and not the word true.

Sorry, I did not know that true was a reserved word in Java.

michaelk 05-20-2022 10:49 AM

Code:

while true; do
...
done

Basically this is a continuous loop. It will run until the OP enters a N or n.
Code:

read $file >> ${TARGET_FILE}
It is not exactly clear where you are having a problem? Where is TARGET_FILE defined? What are you trying to accomplish with this command? If you are just trying to copy the file try:

Code:

cp $file > ${TARGET_FILE}
The single > will overwrite the previous contents.

In addition if you want you can add a default case statement i.e. *) to the end so that just pressing the Enter key will loop without having to enter Y each time.

ntubski 05-20-2022 10:17 PM

As michaelk said, you haven't explained clearly what is going wrong, but I guess the main problem is here:

Quote:

Originally Posted by ortizimo (Post 6355121)
exec ./runpe ${TARGET_FILE}; break;;

exec replaces the currently executing program with its argument, so it never returns. Probably you want just ./runpe ... instead.

grail 05-21-2022 01:37 AM

I am with the others above in that the issue appears to be you have put too much in to the script.

If I have understood the issue, you need the following 3 things removed:

1. the "break" in the 'Y' section is not required as this will break the infinite loop and hence your script will only run once

2. the "file" variable and replace it with TARGET_FILE

3. the exec, as above this is not required to run the program you have listed

So the new copy would be:
Code:

while true; do
  read -p "Do you want to scan another file? (y\n) " yn
  case $yn in
    [Yy]* ) unset TARGET_FILE
            ls -h
            read -p "Enter the name of the file: " TARGET_FILE
            ./runpe "$TARGET_FILE";;
    [Nn]* ) break;;
  esac
done

If you want to be really nice to your users, have a look at the "select" command (help select)

MadeInGermany 05-21-2022 05:50 AM

unset $TARGET_FILE
does not make sense, you certainly want
unset TARGET_FILE
and you can omit it, because you redefine TARGET_FILE anyway.

pan64 05-21-2022 02:31 PM

Quote:

Originally Posted by jailbait (Post 6355126)
Sorry, I did not know that true was a reserved word in Java.

true is not only a reserved word in shells (like bash, zsh), but also an executable (and it also has a short format, a :)
Code:

$ type true
true is a shell builtin
$ type :
: is a shell builtin
$ which true
/bin/true


ortizimo 05-23-2022 07:00 AM

thank you all for your replies. Thanks to Grail and MadeInGermany...it did help and its doing what I wanted it to do. Thanks for the lesson!!!


All times are GMT -5. The time now is 03:16 AM.