Error while reading from a file in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error while reading from a file in shell script
# 1  
Old 02-15-2012
Error while reading from a file in shell script

Hi All,

I'm writing a script to read a file line by line and then perform awk function on it. I am getting an error . My file has one name in it "James". I'm expecting my o/p to be youareJamesbond
James
./users.sh: line 7: =: command not found

Code:
#script to read file line by line
#adding quotes at start and end of users
#!/usr/bin/bash

while read line
    do echo $line
       $username=$(awk '{print "youare" $line "bond"}')
       echo $username
done</home/gunner/users.txt
echo $username

# 2  
Old 02-15-2012
You don't use $ when assigning a variable.

Code:
username=$(awk '{print "youare" $line "bond"}')

You don't need awk for this, however! Never use external utilities for things you can do with builtins, especially when processing individual lines. Running awk 9,000 times for 9,000 lines is like making 9,000 phonecalls to say 9,000 words.

You don't need another variable for this, either. I'm guessing awk got resorted to because "youare$linebond" tries to use the variable $linebond and not $line. You can surround the variable with {} brackets to prevent this.

Code:
while read line
do
        echo $line
        echo "youare${line}bond"
done</home/gunner/users.txt

# 3  
Old 02-15-2012
Lose the $ on $username in this context.
Code:
username=$(awk '{print "youare" $line "bond"}')

And as Conona688 noticed $line will never get expanded becuase it is between two single quote characters and the "awk" was not needed.
Code:
username="youare${line}bond"

# 4  
Old 02-15-2012
Thank you all, that worked. I was able to get awk working also well spotted methyl. I'm still new to scripting in shell so not really aware of perfomance tuning. Thanks for the tip though.

Cheers
# 5  
Old 02-15-2012
Quote:
Originally Posted by Irishboy24
I'm still new to scripting in shell so not really aware of perfomance tuning.
It's not "tuning", the difference between shell internals and external utilities is very important. Imagine if you were loading and quitting your web browser every time you wanted to browse to a different web page. That's what running awk for one line is like. It can process 10,000 lines far faster than a shell could, but takes time to load and quit.

You need to learn what builtin operations your shell has to write decent code. The advanced bash scripting guide can be very useful even if you weren't using bash, but is especially useful since you are. Check out the 'reference cards' section especially.
# 6  
Old 02-15-2012
Thanks for the link mate. I'll check this out . As always appreciate your time on this one.

Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error in reading variable in shell script

Hello all, I have small script: # SCRIPT COMMONFILEPATH=$WORKDIR/samples/... (4 Replies)
Discussion started by: emily
4 Replies

2. Shell Programming and Scripting

Shell script reading file slow

I have shell program as below #!/bin/sh echo ======= LogManageri start ========== #This directory is getting the raw data from remote server Raw_data=/opt/ftplogs # This directory is ready for process the data Processing_dir=/opt/processing_dir # This directory is prcoessed files and... (4 Replies)
Discussion started by: Chenchireddy
4 Replies

3. UNIX for Dummies Questions & Answers

C-Shell script help reading from txt file

I need to write a C-Shell script with these properties: It should accept two arguments on the command line. The first argument is the name of a file which contains a list of names, and the second argument is the name of a directory. For each file in the directory, the script should print the... (1 Reply)
Discussion started by: cerce
1 Replies

4. Shell Programming and Scripting

Reading a csv file using shell script

Hello All, I have a csv file that looks like below ProdId_A,3.3.3,some text,some/text,sometext_1.2.3 ProdId_B,3.3.3,some text,some/text,sometext_1.2.3 ProdId_C,3.3.3,some text,some/text,sometext_1.2.3 ProdId_A,6.6.6,some text,some/text,sometext_9.9.9 I will get ProdId from... (5 Replies)
Discussion started by: anand.shah
5 Replies

5. Shell Programming and Scripting

Reading arguments for a shell script from file

I have a shell script that takes 2 arguments. I will have to execute this script multiple times with different values for the arguments. for example, ./shscript env1 value1 ./shscript env1 value2 ./shscript env2 value3 ./shscript env3 value4 ./shscript env1 value5 ./shscript env3... (24 Replies)
Discussion started by: goddevil
24 Replies

6. Shell Programming and Scripting

Reading the Properties File From Shell script

Hi, I am new to the shell script please I need help for following question. I have properties file name called "com.test.properties" I have No of key values in this properties. com.person.name = xyz com.person.age = 55 com.person.address = hello I want read this properties but i... (1 Reply)
Discussion started by: venukjs
1 Replies

7. Shell Programming and Scripting

File reading problem via shell script

Hi, Data file named parameter contains : DB=y Alter_def.sql Create_abc.sql SQL=y database.sql my_data.sql To read this file I use var_sql=$(awk -F= '$1 == "SQL" { print $2 }' parameter.txt) if then sql_f_name=`grep "\.sql" parameter.txt` echo $sql_f_name fi (2 Replies)
Discussion started by: Dip
2 Replies

8. Shell Programming and Scripting

file reading through shell script

For reading a file through shell script I am using yhe code : while read line do echo $line done<data.txt It reads all the line of that file data.txt. Content of data.txt looks like: code=y sql=y total no of sql files=4 a.sql b.sql c.sql d.sql cpp=n c=y total no of c files=1 (4 Replies)
Discussion started by: Dip
4 Replies

9. Shell Programming and Scripting

Reading data from a file through shell script

There is one Text file data.txt. Data within this file looks like: a.sql b.sql c.sql d.sql ..... ..... want to write a shell script which will access these values within a loop, access one value at a time and store into a variable. can anyone plz help me. (2 Replies)
Discussion started by: Dip
2 Replies

10. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question