LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-03-2007, 06:24 AM   #1
bhandu
LQ Newbie
 
Registered: Mar 2006
Posts: 22

Rep: Reputation: 15
Exclamation 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?
 
Old 07-03-2007, 07:52 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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)
 
Old 07-03-2007, 07:53 AM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
 
Old 07-03-2007, 08:55 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 07-03-2007, 10:24 AM   #5
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
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
 
  


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
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM
Error running postrotate script... Jake_B Red Hat 0 09-19-2005 05:34 PM
libmysqlclient error when running a php script rose_bud4201 Linux - Software 2 06-03-2005 02:43 PM
error running script on crontab haora Linux - Newbie 3 03-11-2005 05:02 PM
cron error when running script? meeble Linux - Software 3 09-09-2004 06:00 PM


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