LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training 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 11-09-2010, 08:56 PM   #1
mark.cooray
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Read a line of C comamnd and execute in Shell Script


Hi,

I am using following code to read a lien from a file and store the value to a variable $line and execute it.

while read line
do
./$line &
done <POOL-original

Eg: of the read line (a C program execution) : Col -fh </home/research/mbcooray/TY/generator/FILES/col1

Col = C program
-fh = arg1
/home/research/mbcooray/TY/generator/FILES/col1 = arg2

====================================================

Problem is when i debug I see that the command Col -fh </home/research/mbcooray/TY/generator/FILES/col1 is replaced by Col -fh '</home/research/mbcooray/TY/generator/FILES/col1'

Hence

it gives me an error ./Col: bad option </home/research/mbcooray/TY/generator/FILES/col7

Please help me...
 
Old 11-09-2010, 11:29 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Try a for loop, setting IFS to a newline http://tldp.org/LDP/abs/html/loops1.html#BINGREP http://tldp.org/LDP/abs/html/internalvariables.html
 
Old 11-10-2010, 12:23 AM   #3
mark.cooray
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Read a line of C comamnd and execute in Shell Script

I used below -

IFS=" "
# The space character will be interpreted as a separator.
# The comma reverts to default interpretation.
echo $var1 # a+b+c
echo $var2 # d-e-f
echo $var3 # g,h,i

AND Replaced :

#!/bin/bash
# fs.sh
set -x

while read line
do
IFS="'"
test=$line
./$test
done <POOL-original

The debugger shows below

+ read line
+ IFS=''\'''
+ test='Col -h </home/research/mbcooray/TY/generator/FILES/col7'
+ './Col -h </home/research/mbcooray/TY/generator/FILES/col7'
./readFile: line 9: ./Col -h </home/research/mbcooray/TY/generator/FILES/col7: No such file or directory

BUT if I use the command ./Col -h </home/research/mbcooray/TY/generator/FILES/col7 in the general console it works

I see that that the IFS="'" have removed the '' from the middle of the file and places it at the start and end.

Does not solve my problem

Please help
 
Old 11-10-2010, 01:32 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Show the content of POOL-original. Note also that I said set the IFS to a newline as per that link
Quote:
IFS=$'\012' # Per suggestion of Anton Filippov.
# was: IFS="\n"
and use a for loop, not a while loop. also, you seem to have set it to a space, then a backslash.
Incidentally, try

set -xv

for more detailed debugging in your script.
 
Old 11-10-2010, 07:04 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
If I understand what you're doing, it seems like the "eval" builtin is what you're looking for.
Code:
$ mycmd='echo stuff with shell characters; cat </dev/null; echo "1 plus 2 is" $((1+2))'
$ echo $mycmd
echo stuff with shell characters; cat </dev/null; echo "1 plus 2 is" $((1+2))
$ eval "$mycmd"
stuff with shell characters
1 plus 2 is 3
$
 
Old 11-11-2010, 09:17 PM   #6
mark.cooray
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks
 
Old 11-11-2010, 10:22 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
eval can be dangerous; it would be safer to change the Col program to read the name of its input file from its argument list.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
shell script read line from file, use it in command DiGiGoth Programming 5 08-31-2008 12:08 PM
shell script that read each line separatly xpucto Programming 6 09-20-2005 09:06 AM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 10:16 AM


All times are GMT -5. The time now is 12:51 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