LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2022, 09:59 AM   #1
ortizimo
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 05-20-2022, 10:17 AM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Quote:
Originally Posted by ortizimo View Post

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.

Last edited by jailbait; 05-20-2022 at 12:15 PM.
 
Old 05-20-2022, 10:49 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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.

Last edited by michaelk; 05-20-2022 at 10:54 AM.
 
Old 05-20-2022, 10:17 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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 View Post
exec ./runpe ${TARGET_FILE}; break;;
exec replaces the currently executing program with its argument, so it never returns. Probably you want just ./runpe ... instead.
 
Old 05-21-2022, 01:37 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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)

Last edited by grail; 05-21-2022 at 11:41 PM. Reason: Corrected unset as I missed it
 
1 members found this post helpful.
Old 05-21-2022, 05:50 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
unset $TARGET_FILE
does not make sense, you certainly want
unset TARGET_FILE
and you can omit it, because you redefine TARGET_FILE anyway.
 
1 members found this post helpful.
Old 05-21-2022, 02:31 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by jailbait View Post
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

Last edited by pan64; 05-21-2022 at 02:43 PM.
 
1 members found this post helpful.
Old 05-23-2022, 07:00 AM   #8
ortizimo
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
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!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
recovering "lost" users..... probably stupid question (stupid user.......) bigjohn Linux - Newbie 6 11-07-2009 06:51 PM
which is true? more than one could be true? ddomiray Linux - General 3 10-26-2009 02:41 AM
always true while loop JDska55 Linux - Newbie 2 08-17-2009 04:40 PM
Stupid Dumb Stupid Question... drigz Linux - Software 3 09-23-2004 03:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration