Register
It is currently Mon Dec 22, 2014 2:10 pm

Scripts within Scripts


All times are UTC - 6 hours


Post new topic Reply to topic  [ 14 posts ] 
Author Message
 PostPosted: Tue Oct 05, 2010 10:50 am   

Joined: Tue Oct 05, 2010 10:42 am
Posts: 1
All,

I'm relatively new to this and probably do a lot of things in very backwards and incorrect ways, but any help that could be offered would be greatly appreciated.

I have two scripts that I use to update Linux machines remotely, the first is a scheduled to a cron job that runs every hour, and it wget's the second file which actually contains the patch, runs it, and then deletes it. It also directs the output of the patch to a file.

The tricky part comes in the patch itself, as it uploads the output of itself to a website(I know...). The upload command is at the end of the patch, but the upload is still failing due to the output not being written yet.

So, is it possible to have the output write to the file as it occurs rather then letting the script finish? I can provide any code needed if this is to vague or just doesn't make sense.

Thanks in advance,

-Tim


Top
 Profile  
 PostPosted: Tue Oct 05, 2010 11:50 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Just put everything except the 'upload' part of your script between curly brackets and redirect the output to a logfile. Then as a last line do your 'upload'. (and/or delete the logfile)

Code:
#!/bin/bash

{
  echo "This is the output of my script"
  echo "It contains quite some output"
  echo "Don't be alarmed by it"
} > output.log

cat output.log


Top
 Profile  
 PostPosted: Wed Oct 06, 2010 10:12 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
grrr, there is no good reason for using cat in bash scripts!
Code:
echo "$(<output.log)"


Top
 Profile  
 PostPosted: Wed Oct 06, 2010 11:51 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
It was meant as an example, so he could replace the 'cat' command with whatever he's using to post the logfile with.


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 3:41 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Watael wrote:
grrr, there is no good reason for using cat in bash scripts!
Code:
echo "$(<output.log)"



o_O
You have to be taking the piss, if all you want to do is output the file to screen then cat is fine. How do the extra 7 characters in your example improve function, clarity or beauty?


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 6:06 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
man bash says :
Quote:
The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 10:12 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
That's what the man page says but it does not say that 'echo "$(<output.log)"' is faster than 'cat output.log' when all you want to do is output the data, and the cat command is much clearer.

It is true that you can use whatever method you think is best but I personally would use 'cat file' unless I was doing further processing on the data. I think it's easier to understand, not only for beginners but also for others reading your script :)


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 10:14 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Of course in the actual example given earlier I would have used tee instead :)

Code:
{
  echo 'my code here'
} | tee output.log


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 11:52 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
I would have let him pipe the output directly to his 'upload' command as well, if I was sure that there wouldn't be any other commands in between.


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 12:19 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
Quote:
That's what the man page says but it does not say that 'echo "$(<output.log)"' is faster than 'cat output.log' when all you want to do is output the data, and the cat command is much clearer.
calling an external command is always slower.


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 1:10 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
You should be careful what you preach ;)

Code:
$>cat internal.sh external.sh
#!/bin/bash

for i in $(seq -w 10000); do
  echo "$(</etc/passwd)"
done > /dev/null

#!/bin/bash

for i in $(seq -w 10000); do
  cat /etc/passwd
done > /dev/null

$>time ./internal.sh
real    0m12.311s
user    0m7.710s
sys    0m5.100s

$>time ./external.sh
real    0m5.592s
user    0m2.190s
sys    0m3.760s


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 2:15 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
indeed. :(

but,
Code:
time for i in {0..10000}; do cat /etc/passwd; done > /dev/null

real   0m19.828s
user   0m5.924s
sys   0m13.817s


time { var="$(</etc/passwd)"; for i in {0..10000}; do echo "$var"; done > /dev/null; }

real   0m14.294s
user   0m13.913s
sys   0m0.288s
the second is faster because it doesn't open the file each time.


Top
 Profile  
 PostPosted: Thu Oct 07, 2010 2:40 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
But that's cheating! :) The second scripts only reads the contents of the file once and prints it 10000 times. Not read and print the file 10000 times, which is what 'cat' does. And we were talking about that :)


Top
 Profile  
 PostPosted: Mon Oct 11, 2010 2:17 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Watael wrote:
grrr, there is no good reason for using cat in bash scripts!
Code:
echo "$(<output.log)"



he he he, well.... While I would agree that cat is used way more than necessary.... I'd disagree that there is no good reason to use cat in a bash script...
Here are some.

An extremely simple method to write to a new file.
cat > somefile
<enter information here>
<when done CTRL-D>

Create an actual line numbered copy of your file
cat -n somefile num_somefile

View special characters that might be in a file
cat -v somefile
<this shows CTRL chars etc>

The normal use
cat file1 file2 file3 > newfile

print file1 and file2 to standard output
cat file1 file2


And for clarity.... I ALWAYS use
cat filename | sed/awk/grep .....especially if I have multiple pipes... it's just easier to read. I understand it isn't technically correct... but it is much clearer to read, not to mention less to type.

Purists tend to stay away from using cat, they also tend to stay away from sed/awk and use bash parameter substitution too. But neither make it easier to understand what the program is actually doing. As a systems administrator, I would prefer to see/read a script that is documented and easy to understand quickly...but that's just me :)

However, if I was writing something that was going to be released on sourceforge or something like that, I'd definitely tidy up my code ;)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP