LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-16-2012, 09:39 PM   #1
Arodef
Member
 
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Rep: Reputation: 17
Need help with script writing: Storing cmd in variable, print variable, then exe cmds


I have a script which contains sections that store a command to a variable, prints out the command, then executes it.

Example:
Code:
MYCOMMAND="cp file1.txt $NEWFILE"
echo "Running $MYCOMMAND"
$MYCOMMAND

The above works when I save a single command to the string like I did above. However I want to store a string made up of multiple commands separated by && or ;. Example:

Code:
MYCOMMAND="cp file1.txt $NEWFILE && rm file1.txt && echo done"
echo "Running $MYCOMMAND"
$MYCOMMAND
When I separate the commands with && or ;, the whole string is echoed correctly, but only the first command actually gets executed. So the cp in the above example get executed, the rm and 'echo done' are not run.

How I can save a string with multiple commands in it to a single variable, echo that variable so the commands that are going to be run are printed out first, and then actually run the commands stored in that variable?
 
Old 01-16-2012, 10:31 PM   #2
fsainsbu
LQ Newbie
 
Registered: Aug 2010
Posts: 5

Rep: Reputation: 1
NEWFILE=emp.2.txt
MYCOMMAND="cp file1.txt $NEWFILE ; rm file1.txt ; echo done"
echo "Running $MYCOMMAND"
echo $MYCOMMAND |sh
 
1 members found this post helpful.
Old 01-17-2012, 12:50 AM   #3
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 668

Rep: Reputation: 134Reputation: 134
You can use 'set -x'

Either this way if you want to trace every command in your script:
Code:
#!/bin/bash -x

your script
Or this way for only selected commands:
Code:
#!/bin/bash
...
commands
...
set -x
# those commands will be traced
commands
set +x

# those commands will not be traced
commands
 
Old 01-17-2012, 01:26 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by fsainsbu View Post
NEWFILE=emp.2.txt
MYCOMMAND="cp file1.txt $NEWFILE ; rm file1.txt ; echo done"
echo "Running $MYCOMMAND"
echo $MYCOMMAND |sh
Nice technique

But, if any of the command arguments include whitespace (space, tab or newline), it will fail. More information about wanting to execute complex commands held in a bash variable in Greg's Bash FAQ.
 
  


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
Trouble storing a variable in a shell script after awk output uncle-c Linux - Newbie 3 02-08-2010 08:13 AM
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 08:59 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 06:21 PM
Trying to write a perl script that will print shell variable ohcarol Programming 2 04-16-2007 09:02 AM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 04:57 AM


All times are GMT -5. The time now is 05:17 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration