LinuxQuestions.org
Help answer threads with 0 replies.
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 06-06-2012, 02:25 AM   #1
asiddique2u
LQ Newbie
 
Registered: Sep 2011
Location: bangalore
Distribution: ubuntu, centos
Posts: 28

Rep: Reputation: Disabled
korn shell:command not interpreting variable value correctly


Hi,

I am trying to write a script which will run a program. I want to run the program in background if specific variable is set. I took '&' symbol in variable and tried to use that variable in cmd but it didn't work. I also tried '\' and eval on that variable it is also not working. I thing shell is not interpreting it correctly.

Can somebody help me out please?!

PS: I am using rhel 6.1
 
Old 06-06-2012, 02:29 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,189

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
you would show your code or at least a part of it. I'm sure there is no problem with the shell.
 
Old 06-06-2012, 02:38 AM   #3
asiddique2u
LQ Newbie
 
Registered: Sep 2011
Location: bangalore
Distribution: ubuntu, centos
Posts: 28

Original Poster
Rep: Reputation: Disabled
Thanks for the reply..
.
.
if [[ ${BACKGROUND} -eq 1 ]];then
bg='&' # it is working
fi
.
.
${prog} >> ${tmp_log} 2>&1 ${bg} # this is not working

The program is running but it is in foreground.

I also tried

cmd="${prog} >> ${tmp_log} 2>&1 ${bg}"
eval ${cmd}
${cmd}

didn't work


Here's the code:



if [[ ${BACKGROUND} -eq 1 ]];then
bg='&'
fi
cd ${WORKLOADS}
for file in ${load_file[*]} ; do
prog="${PROG} -f tmp/${file##*/}"

#
# Run
#
${prog} >> ${tmp_log}$i 2>&1 ${bg}
tmp=$!
sleep 2
tmp=`ps -aef | grep ${tmp}\
| awk -v var=${tmp} '{if ( $3 == var ) print $2;}'`
pid="${pid} ${tmp}"
i=$i+1
fi
done

Last edited by asiddique2u; 06-06-2012 at 02:44 AM. Reason: code
 
Old 06-06-2012, 02:47 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,189

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
you can try to insert an echo $cmd before eval $cmd to see what is in it.
 
Old 06-06-2012, 02:55 AM   #5
asiddique2u
LQ Newbie
 
Registered: Sep 2011
Location: bangalore
Distribution: ubuntu, centos
Posts: 28

Original Poster
Rep: Reputation: Disabled
I tried it. It has everything correctly

I tried echo cmd=${cmd}
and the output was

cmd=/prog -f tmp/file >> /tmp/log1 2>&1 &
 
Old 06-06-2012, 03:14 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,189

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
You can try to put set -xv at the beginning of the script. And maybe your prog will report some error message.
$cmd does not work for me, but eval $cmd works.
 
Old 06-06-2012, 03:23 AM   #7
asiddique2u
LQ Newbie
 
Registered: Sep 2011
Location: bangalore
Distribution: ubuntu, centos
Posts: 28

Original Poster
Rep: Reputation: Disabled
I got following output with set -xv

+ eval /prog -f tmp/file '>>' /tmp/file '2>&1' '&'
+ /prog -f tmp/file
+ 1>> /tmp/log1 2>& 1

I guess eval is doing some operation based on &.
 
Old 06-06-2012, 03:34 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,189

Rep: Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673Reputation: 2673
the problem is that ${bg} will be evaluated and the result will be passed as argument to your prog. If this prog had a good argument parsing you would see the last argument: &. Eval will evaluate the string $cmd therefore ${bg} will be sustituted, so at execution the shell will interpret the & sign.
you can also try: eval cmd='${prog} ..... ${bg}' and eval $cmd
 
Old 06-06-2012, 04:09 AM   #9
asiddique2u
LQ Newbie
 
Registered: Sep 2011
Location: bangalore
Distribution: ubuntu, centos
Posts: 28

Original Poster
Rep: Reputation: Disabled
Got it. Yeah. You were right. It is going as an argument. So I passed that variable seperately to eval "eval $cmd $bg" and it worked.

Thanks a lot for your help.

cheers.
 
Old 06-06-2012, 12:08 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


No, no, no! How many times must we stress it, NEVER use eval unless it's absolutely necessary, there are truly no other options, and you know exactly at all times what the code is doing!

Eval command and security issues
http://mywiki.wooledge.org/BashFAQ/048

Short form, if the command line that uses eval has in it any variables or other substitutions that expand to unknown, uncontrolled values, then malicious or even accidentally damaging code can be run. Only if all the values in the string and their resulting effects are known should you ever even consider it.

But it's a very rare case that you should need it anyway, as there are nearly always better alternatives.


The real problem in this case is that redirections and backgrounding are read at the beginning of the parsing order, before variable expansion happens; so trying to use a variable to set it will never work without the double parsing of eval.

A proper way to implement this, however, would be to to create a function which tests the background condition first and then runs the command with the proper options in place.

Code:
runmycommand() {

	local bg=$1
	shift

	case "$bg" in

		0) mycommand "$@"   ;;

		1) mycommand "$@" & ;;

	esac

}

background=1
runmycommand "$background" option1 option2

Last edited by David the H.; 06-06-2012 at 12:17 PM. Reason: expanded function code
 
  


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
korn shell variable help 4play Programming 9 11-23-2011 12:03 PM
Execute shell command in a variable hankta Linux - Newbie 4 08-25-2011 01:06 AM
Korn Shell syntax issue for remote complex command. toordog Programming 14 04-02-2011 03:31 AM
Output a C variable with a shell command paliga Programming 4 07-20-2010 06:10 AM
executing Bourne shell variable as command? ocicat Programming 3 07-31-2007 02:00 AM


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