Reading of variable in a single line command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading of variable in a single line command
# 1  
Old 01-17-2013
Reading of variable in a single line command

Hi All,

Below is a sample command that I can run without any problem in the command line.

Command Line
HTML Code:
dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt
-rw-r--r--  1 monuser oinstall 0 Jan 18 11:02 ./filename_20130118.txt
But once I put that command line in file (list.txt) and read from it, an error occurs. Pls. see below the code:

HTML Code:
cat ./list.txt|while read xLine
do
echo "`echo $xLine | awk -F \"|\" '{print $2}'`"
echo "========="
`echo $xLine | awk -F "|" '{print $2}'`
done

==========
[B]Result:[/B]

dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt
=========
./j.sh: line 6: dtToday=`date: command not found

The list.txt contains the line below:
HTML Code:
SomeText|dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt
Need help badly to resolve this issue. Thanks in advance.
# 2  
Old 01-17-2013
You will have to change the script file. BTW this is a less than useful way to run scripts because it negatively impacts performance. And prevents the use of variables defined in the input text file.

every "command " you read in you have to preface with
Code:
eval

Please consider reading about it before you cause more problems for yourself.

example:
Code:
while read cm
do
   eval "cmd"
done < input_file.txt

Note that when you do that you cannot have a variable set up in line 1 of input_file.txt
and then read the next line and use that variable. This is because each eval "line of text" runs in a separate child process. No variables are preserved in the current process. Each line is a separate world of its own.
# 3  
Old 01-18-2013
Thanks Jim....

Just to share to everyone the updated code using eval:

Code:
cat ./list.txt|while read xLine
do
cmd=`echo $xLine | awk -F "|" '{print $2}'`
echo "========="
eval "$cmd"
done

# 4  
Old 01-18-2013
That is a useless use of cat and will prevent variables set inside the loop from being used outside the loop.

There is also no point using awk to split variables here when read can do that by itself.

There's also no point using awk 10,000 times to process 10,000 lines when awk can do that all at once. It's like making a hundred phonecalls to say a hundred individual words.

This should do the job with less fuss:

Code:
while IFS="|" read -r A B C D E
do
        echo "========="
        eval "$B"
done < inputfile

p.s.: The -r is probably necessary either way to read shell code. Without it, code containing backslash characters may be mangled.

Last edited by Corona688; 01-18-2013 at 02:03 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

2. Shell Programming and Scripting

Passing Command Line Args in a Single Variable?

Hello All, I have a Bash Script and an Expect script that together will SSH to another server and do some stuff there... From within the Bash Script I process the Command Line Arguments, which are Required Args and Optional Args. When I call the Expect script from the Bash Script, I pass... (4 Replies)
Discussion started by: mrm5102
4 Replies

3. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

4. Shell Programming and Scripting

Input two variable on single line

Can we input two variable on single line that separate by space example user input "list jpg" it will list all jpg files in current directory (3 Replies)
Discussion started by: guidely
3 Replies

5. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

6. Shell Programming and Scripting

Reading a single character from each line of the file

Hi, I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below: while read line ; do single_char=`???` echo "$single_char" done < $input_file (8 Replies)
Discussion started by: arsii
8 Replies

7. Shell Programming and Scripting

reading the whole line from a file into a variable

Hi, I am doing : while read line do printf "%s\n" ${line} done <datafile.txt but I am not getting each single line from the data file assigned to the variable line (but only tokens/fields at a time). I also tried while IFS= read -r lineI want the whole line assigned or read into the... (2 Replies)
Discussion started by: shri_nath
2 Replies

8. UNIX for Dummies Questions & Answers

reading a line into a variable in unix

hi... i need to open a file and read it line by line, and capture that it in to some variable and manipulate... i need to get a line in to a variable please help :confused: (1 Reply)
Discussion started by: lmadhuri
1 Replies

9. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies

10. Shell Programming and Scripting

reading a single line

hello, i need an algorithm which reads a line specified to it in shortest possible time. i am already using sed, i need a better way. please suggest me alternatives (2 Replies)
Discussion started by: rochitsharma
2 Replies
Login or Register to Ask a Question