LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-15-2006, 07:22 AM   #1
realos
Member
 
Registered: Jul 2002
Location: Germany
Distribution: Redhat 7.3, Debian 3.1, Knoppix 3.1, Ubuntu 6.10
Posts: 113

Rep: Reputation: 15
Bash: how to test for a number in Array within if loop?


---------------------------file.txt-----------------------------------
0
34
71
77
152
159
163
293
/END file.txt-------------------------------------------------------

I like to define an if loop with two possible branches depending on whether an integer is found in the error_block.txt file or not.

if [integer in file.txt]
then
command1;
else
command2;
fi

Following is my try at solving this problem but it does not work.

--------------------------------- myscript.sh----------------------------
/skipping

grep '^error' tracefile.txt | awk '{print $3}' > file.txt;

temp='cat file.txt | mawk '/34/{++count}END{print count}''

if [ $temp = 1 ]
then
printf "temp = 1";
else
printf "temp = 0";
fi

/END of myscript.sh------------------------------------------------

Q2) I like to avoid writing to file.txt file for speed purpos. How would I redirect output of the first command from myscript.sh to an array and use that array for testing in if construct.

Please note, that I am not just testing for occurence of a single number (namely 34) only. It is just a test. There is a while-do-done loop around the if construct that delivers different variables to be tested again their appearence in the file.txt.

Hope I could explain my problem. If something is not clear, please ask. I'd greatly appreciate your ideas.

Regards,
 
Old 12-15-2006, 08:59 AM   #2
int0x80
Member
 
Registered: Sep 2002
Location: Cincinnati
Distribution: Debian GNU/Linux
Posts: 310

Rep: Reputation: 31
Bash script:
Code:
#!/bin/bash
# findnum.sh by int0x80
# check if any numbers in the list exist in file

numbers="34 99 152 293 347";
for n in $numbers; do
    echo -n "$n?    ";
    if ! grep -q "^$n\$" $1
    then
        echo "No";
    else
        echo "Yes";
    fi
done
Usage:
Code:
int0x80:~/source/bash/fileio$ ./findnum.sh numbers.txt
34?     Yes
99?     No
152?    Yes
293?    Yes
347?    No
The numbers.txt file:
Code:
int0x80:~/source/bash/fileio$ cat numbers.txt
0
34
71
77
152
159
163
293
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BASH: Reading long filenames into an array using a loop DaneM Programming 12 09-11-2009 08:24 AM
How to extract data and for loop it into an array? (shell) WeiSomething Programming 6 11-17-2006 04:27 AM
Do/while loop and if statements not functioning properly w/ array... ohfaney Programming 5 05-02-2006 03:22 AM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 04:59 PM
repeated number in a 4x4 array xbaez Programming 3 11-22-2004 12:03 AM


All times are GMT -5. The time now is 05:30 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration