Execution Problems with bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execution Problems with bash script
# 1  
Old 04-09-2011
Java Execution Problems with bash script

Hello,

can someone please help me to fix this script,
I have a 2 files, one file has hostname information and second file has console information of the hosts in each line, I have written a script which actually reads each line in hostname file and should grep in the console file and paste the information in the third file, but not sure, I am not getting the desire o/p in third file it is just a blank file.

and Consoles file has 5000 line and hostname file has 500 line

I was writing these scripts non of them working please suggest.
Code:
#!/bin/bash
while read line
do
        grep -i '$line' /scripting/consoles >> /scripting/output
done < /scripting/hostlist.txt

Code:
#!/bin/bash

/bin/date > /scripting/found.txt
/bin/date > /scripting/notfound.txt

while read line

do
  echo "checking" $line;
  sleep 2
  if [[ `/bin/grep $line /scripting/consoles` != "" ]];
    sleep 1
    then
    echo "$line found in /scripting/consoles file"
    echo "$line" >> /scripting/found.txt
    sleep 1
  else
    echo "$line  not found in /scripting/consoles file"
    echo "$line" >> /scripting/notfound.txt
  fi
done < /scripting/hostlist.txt

Thanks,

Last edited by Scott; 04-09-2011 at 07:08 PM.. Reason: Code tags; indentation
# 2  
Old 04-09-2011
Hi.

From your first script, change:
Code:
grep -i '$line'

to
Code:
grep -i "$line"

(i.e. with double quotes, to allow the shell to expand $line (it won't do that in single quotes)).

In your second script, you have a sleep 1 before then. If you need that, put it after the then of before the if.

Moreover, perhaps post a sample of both the input files (using [code][/code]-tags, please).
This User Gave Thanks to Scott For This Post:
# 3  
Old 04-09-2011
Thanks for the reply

but I still have issues, third file is still empty.
and when I tried to debug the script, This is what I notice.
Code:
+ read line
+ grep -i $'pmx1app1\r' /scripting/consoles
+ sleep 1
+ read line
+ grep -i $'drmx1app1\r' /scripting/consoles
+ sleep 1
+ read line
+ grep -i $'pmx1app2\r' /scripting/consoles
+ sleep 1
+ read line

Here is input file entries
hostname file
Code:
pmx1app1
drmx1app1
pmx1app2
drmx1app2

Console file entries
Code:
pmx1app1 tlmcs001.us.net 5050
drmx1app1 tlmcs001.us.net 5051
pmx1app2 tlmcs001.us.net 5052
drmx1app2 tlmcs001.us.net 5053


Last edited by Scott; 04-09-2011 at 07:30 PM.. Reason: Please use code tags
# 4  
Old 04-09-2011
Code:
$ grep -if hostfile console
pmx1app1 tlmcs001.us.net 5050
drmx1app1 tlmcs001.us.net 5051
pmx1app2 tlmcs001.us.net 5052
drmx1app2 tlmcs001.us.net 5053

The output is the same, so perhaps a more representative sample of the data would be helpful?

Code:
$ awk 'NR == FNR { H[$1]=1; next } H[$1]' hostfile console

# 5  
Old 04-09-2011
I think I confused you,
Input files that I provided are just sample file and host file has around 500 lines, each line is hostname and it should search in console file which as 5000 enteries.

and "grep -if" did not worked
# 6  
Old 04-10-2011
Try to pre-process your hostlist like this:
Code:
....
sed 's/\r//' $hfile | while read line ; do 
...
done
...

which will gt rid of those pesky windowsy carriage returns (\r). Otherwise you're grepping for string AND a carriage return, which not matching anything.

I suggest you use variables for filenames instead of hard-coding them and quote the output of grep in case it's more than one word:
Code:
cfile=/scripting/consoles
if [[ "`/bin/grep $line $cfile`" != "" ]] ; then


Last edited by mirni; 04-10-2011 at 08:17 PM..
# 7  
Old 04-10-2011
As suggested I tried to redirect my hostlist file, I was having some errors.....

Code:
[root@localhost scripting]# bash -x final.sh
+ /bin/date
+ /bin/date
final.sh: line 24: syntax error near unexpected token `('
final.sh: line 24: `done < (sed 's/\r//' $hfile)'
[root@localhost scripting]#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script problems int to binary

Hi, I am trying to do a bash script that convert a decimal number to a binary value, but it doesn't work... To begin, I am just trying to convert a positive number to 8 bits binary. read -p"Entrez un nombre entre -128 et 127 pour l'encoder en binaire: " number binaryValues=(128 64 32 16 8 4 2... (8 Replies)
Discussion started by: Zedki
8 Replies

2. Shell Programming and Scripting

sed problems - Bash Script

Hi I keep getting the following error sed: -e expression #1, char 32: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command whenever I use the following bash script #! /bin/bash... (2 Replies)
Discussion started by: spbr
2 Replies

3. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

4. Shell Programming and Scripting

Copying large files in a bash script stops execution

Hello, I'm new to this forum and like to first of all say hello to everyone. I've got a really annoying problem at the moment. I'm trying to rsync some files (about 200MB with one file of 120MB) from a Raspberry PI with raspbian to a debian server via rsync. This procedure is stored in a... (3 Replies)
Discussion started by: wex_storm
3 Replies

5. Shell Programming and Scripting

Expect script Execution Problems .. Help!!!

Hi Guys, I am writing the expect script which take input from the txt file and check whether that file is present over the Sftp or not.If yes then delete other wise check the next one.. I am able to do comparison online for single file... Please also note still i didn't try to implement... (1 Reply)
Discussion started by: hackerdilli
1 Replies

6. Shell Programming and Scripting

Execution problems with BASH Shell Script

Hi I need help with my coding , first time I'm working with bash . What i must do is check if there is 3 .txt files if there is not 3 of them i must give an error code , if al three is there i must first arrange them in alphabetical order and then take the last word in al 3 of the .txt files... (1 Reply)
Discussion started by: linux newb
1 Replies

7. AIX

SH Script Execution Problems with Cronjob

Hi, I have created a sh script to startup and shutdown the oracle database, when I execute the script thru command line it execute successfully, but when I call the script thru cronjob it does not execute. The scripts are as follows: LOG=/oracle/times.log export ORACLE_SID=prod echo... (6 Replies)
Discussion started by: lodhi1978
6 Replies

8. Shell Programming and Scripting

execution time / runtime -- bash script please help!

Hello, I'm running a bash script and I'd like to get more accurate a runtime information then now. So far I've been using this method: STARTM=`date -u "+%s"` ......... *script function.... ......... STOPM=`date -u "+%s"` RUNTIMEM=`expr $STOPM - $STARTM` if (($RUNTIMEM>59)); then... (6 Replies)
Discussion started by: TehOne
6 Replies

9. OS X (Apple)

Execution Problems with ASU Shell Script

Hello. I have been trying to create a shell script that checks to see if there are software updates and if not, then exit the script. If so, then check to see if a user is logged in. If a user is logged in, it will only install the updates. If a user is not logged in, then it will display a... (3 Replies)
Discussion started by: Talcon
3 Replies

10. Shell Programming and Scripting

bash script execution with a variable in a single line

Let a script needs a variable to execute. For example if i run ./test.sh then it needs a variable as there is a <STDIN> in the script. I want to execute it as in command line. Let test.sh requires a variable name $number I want to execute it by >test number <enter> how is it possible? (1 Reply)
Discussion started by: shoeb
1 Replies
Login or Register to Ask a Question