LinuxQuestions.org
Visit Jeremy's Blog.
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 03-07-2009, 05:51 PM   #1
Dan8080
LQ Newbie
 
Registered: Jun 2006
Distribution: CentOS 4.3
Posts: 25

Rep: Reputation: 15
Print a variable while it's doing the action?


Hi,

I'm pretty new to bash programming and I'm stuck on something. I don't even know if there's a way to do this or if I'm in the wrong direction here. Example script:

Code:
#!/bin/bash
files=`ls`;
How can I store that command in a variable like I have there and at the SAME TIME get it to print itself while it's doing it? If more information is needed about what I'm actually trying to make, I can provide it.

Thanks in advance.
 
Old 03-07-2009, 06:55 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Do you mean something like:
Code:
cmd=/bin/ls
function cmd() { echo "$FUNCNAME: $cmd" > /dev/stderr; $cmd; }
files=$(cmd)

Quote:
Originally Posted by Dan8080 View Post
If more information is needed about what I'm actually trying to make, I can provide it.
It never hurts to clarify what you're trying to accomplish in an OP. Faster and more efficient for everyone IMHO.
 
Old 03-08-2009, 04:21 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Or run the script with the "-x" option.
Code:
#!/bin/bash
set -x  # Switch on printing commands as they are executed
files=`ls`;
set +x  # switch it off again
Or, for one time only, run the script this way:
Code:
bash$ bash -x /path/to/script.sh
 
Old 03-08-2009, 06:31 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I understand exactly what he's asking. He wants the output of the embedded command (ls in this case) to simultaneously be stored in the variable and echo to the standard out (i.e. display normally). And now I'm curious if this can be done myself.

But is there any reason why you can't simply put an "echo $files" line in the script right after setting the variable, so that you can see the output right away?

Code:
files=$(ls)
echo $files
 
Old 03-08-2009, 07:21 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Is is possible to print the output of the command to stderr at the same time.
Code:
lstmp=$(ls /tmp | tee >(cat >&2))
echo $lstmp
But does i make sense? For "debugging" I'd prefer the -x option. For other purposes you can just "echo $lstmp" like you said.
 
Old 03-08-2009, 12:32 PM   #6
Dan8080
LQ Newbie
 
Registered: Jun 2006
Distribution: CentOS 4.3
Posts: 25

Original Poster
Rep: Reputation: 15
Thanks for the help everyone. I'll try to provide more info to explain why I want this.

Here's a piece from my backup script that uses rsync:

Code:
echo "               [ Files are being backed up.]                        "
echo "===================================================================="
fbkres=`rsync --bwlimit=6000 -a -v ${www_path} ${ext_hd_path}Server/html/ --stats --progress --exclude=mysql --exclude=gamedata`

echo "===> Writing log file of results..."

wrtolfl= `echo "===================================== " >> ${ext_hd_path}backup_logs/file-backup-results.txt`
wrtolfl= `echo "File Backup " >> ${ext_hd_path}backup_logs/file-backup-results.txt`
wrtolfl= `echo "Date: $(date +%Y) / $(date +%m) / $(date +%d)" >> ${ext_hd_path}backup_logs/file-backup-results.txt`

# Advanced logging removed
#wrtolfl= `echo "" >> ${ext_hd_path}backup_logs/file-backup-results.txt`
#wrtolfl= `echo "Results: $fbkres" >> ${ext_hd_path}backup_logs/file-backup-results.txt`
#wrtolfl= `echo "" >> ${ext_hd_path}backup_logs/file-backup-results.txt`

wrtolfl= `echo "===================================== " >> ${ext_hd_path}backup_logs/file-backup-results.txt`
wrtolfl= `echo "" >> ${ext_hd_path}backup_logs/file-backup-results.txt`

echo "===> Done..."
Here's the problem - I added the "--progress" because I'd like to actually see this while it runs, but if I get rid of it in the variable then I can't make a log of the stats that rsync outputs at the end as I have been doing. I can't echo it AFTER because there's no point there; I want to see the backup progress live. That's why I'm wondering if there's a way to get variables to "echo while they run." I was planning to just tail the variable and insert the stats that way.

I think Hko's solution to use set -x is exactly what I need, I will test when I am able to - thanks!
 
Old 03-08-2009, 02:05 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Ah! Now it makes sense to me why you ask this.

You want to store the output of rsync in a variable so you can store the output in a log later. But you also want to see the output as it runs, right?

Though "set -x" would more or less do it for you, I think it is not the best solution. In fact, storing the output in a variable and the later echo it to a file is not a very good idea IMO. If many files are backed up the variable may consume quite a lot of memory (the --progress option will make rsync write two lines for every file backed up). Also, the echo command will write it to the file as one single big line (newline-codes are lost).

There is a simple solution to solve all this: use the "tee" program.

Instead of what you are trying to do:
Code:
logvar=`rsync -av --stats --progress /srcpath /destpath`
# ...
echo "$logvar" >/path/to/logfile
You can do this to write to a file and stdout (the terminal) at the same time:
Code:
rsync -av --stats --progress /srcpath /destpath | tee /path/to/logfile
 
  


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
How can I read some kernel variable and print it? awbodbijl Linux - Newbie 2 10-13-2008 04:52 AM
Not able to print variable value in crontab entry naren_0101bits Programming 6 12-10-2007 12:04 PM
how to print variable value in Makefile? George2 Programming 12 04-27-2006 05:12 AM
Perl wont print my Variable!!! socceroos Programming 10 03-15-2006 05:04 PM
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 02:49 PM

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

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