LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 08: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, 09: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-16-2012, 11:50 PM   #3
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
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, 12:26 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 AM.

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
Open Source Consulting | Domain Registration