LinuxQuestions.org
Visit Jeremy's Blog.
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 11-18-2010, 06:20 PM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192

oops Sorry, completely forgot about the yes / no questions.

Try this instead:
Code:
exec 3<$project

while read -r JOBID <&3
do
    <your stuff here>
done

exec 3<&-
Also, you may wish to make the yes / no questions more robust as a user may enter y, Y, yes, Yes or even ok.
Perhaps you should suggest possible answers (help get rid of things like ok) and then look at controlling the case (upper or lower)
 
1 members found this post helpful.
Old 11-18-2010, 06:32 PM   #17
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Here's a somewhat simpler way to do the same thing:
Code:
while read -u 3 -r JOBID
do
    <your stuff here>
done 3<"$project"
 
3 members found this post helpful.
Old 11-18-2010, 08:11 PM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Thanks tuxdev ... hadn't seen that one before
 
Old 11-19-2010, 05:12 AM   #19
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Thanks to you both in posts #16 and #17 - nifty exec tricks to keep in back of mind..
 
Old 11-19-2010, 12:15 PM   #20
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
As far as I can tell, I am still using this script:
Code:
#! /bin/bash
for JOBID in `cat jobs-ids.txt`
do
./make_recovery.sh $JOBID

done
which is kind of auto-manual. It feeds the jobs-ids one by one, but I have to answer with y (if I really want to say y) for any of the questions in the main script, otherwise a hit on enter and I move to the next job id.

As you may know now, I have few questions that should be answered with y or the script will abort.
For the first question:
Code:
echo "Make a recovery def for project: ${project}"
echo "First, look at the project summary."
check
The answer should be y

then the script goes to the second question. This time again, the answer should be y and the output message may or may not contain the statement "NO FILES TO RECOVER".

What I hope to do is to have the first two answers as y automatically, and when the message "NO FILES TO RECOVER" is spelled out, the script should proceed to the next JOBID in the jobs-ids.txt. How is this possible.

~faizlo
 
Old 11-20-2010, 01:22 AM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So change the script to auto those parts and check for your string "NO FILES TO RECOVER".

This would however mean moving away from your for loop in last post.
 
Old 12-02-2010, 05:40 AM   #22
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
Hi again,

so I had to it manually as I described in my last post. But, since this would really help me a lot for all my jobs, I wonder if it is possible to get some help from you to make it fully functional and fully processed without me having to reply with either y or [enter]

How may I start this?
 
Old 12-02-2010, 05:59 AM   #23
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Try:
Code:
#! /bin/bash
for JOBID in `cat jobs-ids.txt`
do
    yes | ./make_recovery.sh $JOBID
done
 
Old 12-02-2010, 06:06 AM   #24
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I would edit the jobs-id.txt file so it looks like:
Code:
ID1 Y Y
ID2 Y Y
This way you can change the while loop:
Code:
while read -r JOBID PROJECT DIMS
do
     <your stuff here and use PROJECT and DIMS to answer first 2 questions if they are there>
done<jobs-id.txt
 
Old 12-02-2010, 07:10 AM   #25
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
So, is there a way to check the output for the certain string "STRING" and decides the answer based on it?
 
Old 12-02-2010, 07:54 AM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Yes you should be able to capture the output in a variable and test its equality against your chosen input:
Code:
if [[ "$VAR_WITH_OUTPUT" == "$VAR_WITH_INPUT_TO_TEST" ]]
then
    <do stuff here if true>
fi
 
Old 12-02-2010, 08:07 AM   #27
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by faizlo View Post
So, is there a way to check the output for the certain string "STRING" and decides the answer based on it?
Yes. Assuming my previous suggestion worked:
Code:
#! /bin/bash
for JOBID in `cat jobs-ids.txt`
do
    case $( yes | ./make_recovery.sh $JOBID) in
        *'first string you are looking for'* )
            <whatever you want when it is found>
            ;;
        *'second string you are looking for'* )
            <whatever you want when it is found>
            ;;
        * )
            <whatever you want when none of the strings are found>
            ;;
    esac
done
The * in front and after the strings makes the case statement match them if they appear anywhere in the output.
 
Old 12-02-2010, 08:10 AM   #28
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
@grail:

the output is 10s of lines, if not 100s, but the final 4 lines contain the string "NO FILES TO RECOVER", so I guess I need grep for that.
I am sorry, but I am getting confused now.
I have tried to google this but did not know which term to use for search, I used "check output for a certain string" but couldn't find what I am looking for.

~faizlo
 
Old 12-02-2010, 08:11 AM   #29
faizlo
Member
 
Registered: Sep 2008
Location: USA
Distribution: Linux Mint Qiana
Posts: 190

Original Poster
Rep: Reputation: Disabled
@catkin:

I will check that.

~f
 
Old 12-02-2010, 08:13 AM   #30
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So which command is returning the 10s / 100s of lines? If you know it will definitely be in the last line then a simple tail command will show you what you want.
 
  


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] Feeding input to interactive programs in bash babaqga Linux - Newbie 4 08-31-2010 10:17 AM
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 06:53 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
Bash script to edit text file snowman81 Linux - Desktop 2 01-10-2007 03:33 PM
Change Text-File through bash script arkus Programming 2 12-17-2004 08:48 PM

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

All times are GMT -5. The time now is 11:45 PM.

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