LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   error in running the script (https://www.linuxquestions.org/questions/linux-newbie-8/error-in-running-the-script-566247/)

bhandu 07-03-2007 05:24 AM

error in running the script
 
Hi,
I have written a script which scans two files and it replaces a
field in the second file with the field present in first file if the same record is present in both the file. Following is the script

cat first |
while read line
do
usr1=`cut -d ":" -f1 line`
pass1=`cut -d ":" -f2 line`
cat sec |
while read line1
do
usr2=`cut -d ":" -f1 line1`
pass2=`cut -d ":" -f2 line1`
if [ $usr1 = $usr2 ]
then
sed -e s/$pass2/$pass1/ line1
break
fi
done
done

But when I execute this script it gives me below error

$ sh scan.sh
cut: Cannot open line.
cut: Cannot open line.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line.
cut: Cannot open line.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line.
cut: Cannot open line.
cut: Cannot open line1.
cut: Cannot open line1.
cut: Cannot open line1.
$

can anyone help me on this?

pixellany 07-03-2007 06:52 AM

Cut looks for a filename to read from (or accepts a stream from a pipe). In your case, you are attempting to read data from a variable. Cut is looking for a file named "line" and finds none.

Also, I think you are using the read command incorrectly. I tried this:
cat filename | read line #this executed with no errors
echo $line #the variable was empty
I don't know what the first line did, but it clearly did not put data into the variable "line".

I think "read" is used to take data from the terminal (or more correctly, standard input)

wjevans_7d1@yahoo.co 07-03-2007 06:53 AM

This seems like a homework problem, but I'll give you a clue anyway.

The error message "cut: Cannot open line1." implies that the program cut is trying to open a file called line1, which I don't believe is what you want.

If you do this at the command line:

Code:

man cut
you'll see why cut is trying to open file line1.

bhandu, you have on occasion posted scripts here. They will be more readable (more accurately) if you click on the [B]Go Advanced[B] button at the bottom of the editing window, enter your question (including the script), highlight the script by dragging your mouse, and then clicking on the # button at the top of the editing window.

pixellany 07-03-2007 07:55 AM

Quote:

This seems like a homework problem, but I'll give you a clue anyway.
Maybe, but does not fit the pattern. Most homework questions are one post only--never to be seen again.

Bhandu; What Scripting books have you read? There are some good free ones at tldp.org

lakris 07-03-2007 09:24 AM

I couldn't help thinking about this, maybe I like homework! You could have a look at this,

Code:

#!/bin/bash

#We assume that the files are strictly formatted, no spaces, empty lines, etc

#use temp files just while we're testing, when we're confident that the script works, use real filenames
cp sec new-sec

#creates an array of passwords from the first file
pass1=(`cut -d ":" -f2 first`)

#the index for the array
cnt=0

#Let's loop over the username entries in the first file
#You don't have to know the contents of file sec, only sedit it
#where it fits a given pattern
for usr1 in `cut -d ":" -f1 first` ; do
        #If a line in the sec file starts with this username,
        #replace the password no matter what it was before
        #It will also keep lines with users that are not in first
        sed -e "/^$usr1:/ s/:.*$/:${pass1[$cnt]}/" new-sec > tmp-sec
        #make sure the result is saved in sec file
        mv tmp-sec new-sec
        #increase counter
        cnt=$(($cnt+1))
done                       
#And just to verify
echo ========Original sec file =============
cat sec
echo ========New sec file =============
cat new-sec

And with comments and temp file handling removed it becomes

Code:

#!/bin/bash
pass1=(`cut -d ":" -f2 first`)
cnt=0
for usr1 in `cut -d ":" -f1 first` ; do
        sed -e "/^$usr1:/ s/:.*$/:${pass1[$cnt]}/" sec > tmp-sec
        mv tmp-sec sec
        cnt=$(($cnt+1))
done



All times are GMT -5. The time now is 05:04 PM.