Programming :: Capture The Stdout Of A Heredoc In A Variable?
Aug 12, 2010
Why doesn't this work?
Code:
cat | myvar=$(</dev/stdin) <<EOS
This is some content
EOS
echo "$myvar"
i have also tried several variants of cat in place of the redirection.
Code:
$(cat -)
$(cat)
$(cat /dev/stdin)
None of the variants print "This is some content"
I have a version that works:
Code:
myvar=$(cat <<EOS
This is some content
EOS
)
echo "$myvar"
but i don't like the syntax and it doesn't play nice in my editor with code folding.
View 5 Replies
ADVERTISEMENT
Jan 17, 2014
I am writing a script that calls a program which writes a lot of lines to stdout continuosly. If the last line in stdout has some regex, THEN, certain variables are updated. My problem is that I don't know how to do that.
A simplified example would be (it's not my exact case, but it I write it here to clarify): suppose I issue a ping command (which writes output to stdout continuously). Every time that the response time is t=0.025 ms, THEN, VARIABLE1=(column1 of that line) and VARIABLE2=(column2 of that line).
I think the following code would work in awk (however, I want the variables in bash and I don't know how to export them)
Code: Select allping localhost |awk '{ if ( $8 == "time=0.025" ) var1=$1 var2=$2}'
In the previous code, awk analyzes each line of the output of the ping command as soon as it is created, so the variables $var1, $var2, ... are updated at the appropriate time. But I need the "real-time" updated values of $var1, $var2 in bash, for later use in the script.
View 7 Replies
View Related
Oct 28, 2010
I am using php 5.3.3 with apache 2.2.16 on gentoo. While using HEREDOC inside php I get "PHP Parse error: syntax error, unexpected T_VARIABLE in /data/www/html/shaper/htb/htb.php on line 13". Whereas same script is working fine with php 5.2.13 and apache2.2.15. Below is the some line of code:
[Code]....
View 5 Replies
View Related
Jun 30, 2009
I want to parse my mail log file and reuse the results but I'm having a hard time structuring the syntax. Something like:
Code:
grep hostname /var/log/mail.log |
grep NOQUEUE: |
sed -e 's/hostname postfix/smtpd/[[0-9]*]: //g'
at this point I want to redirect what I have in hand to a file but also ... fork? or split? whatever the term, to continue onward so that I can pipe the results further into wc -l or sort or programX. without having to re-loop through that huge log file.
View 2 Replies
View Related
Jan 22, 2010
In this example, why does blacklist end up in the file blacklist and $a end up in stdout?
[code]...
The desired result is to have a file containing the results of lsmod which had the first word on the line beginning with snd_ copied into another file preceded by the word blacklist.
View 4 Replies
View Related
Mar 24, 2011
I want to have the output of a program go to 2 different files but not going to standard out. Is there a way to do this in bash? I know that in Z shell its really easy. omething like: Code: echo "test" >> file1 >> file2 Would work. But in Bash it doesn't seem that easy. I know that tee will send the output to 2 files but it also sends it to STDOUT.Something like:Code: echo "test" | tee -a file1 file2 Would put the word "test" in file1, file2, and STDOUT. Is there a way to just send the output to file1 and file2?
View 2 Replies
View Related
May 12, 2009
I have a little complex Makefile system. A parent Makefile call dozens of Makefiles in subdirctories. And the subdirctory Makefile calles shell script to do real building. I want to grab all output this Makefile system generate. So, i employ "make 2>&1 > make.log". but not all output messages are filed into make.log. The message generated by sub-makefile called shell script cannot be recorded into make.log. And another curiouse thing is, if i launch "make 2>&1 > make.log" in a perl script, all output do be sent into make.log.
View 2 Replies
View Related
Aug 21, 2009
I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines.
For example:
Code:
Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).
View 6 Replies
View Related
Apr 7, 2010
my script has a variable which comes in the form +00.00 +0.00 -00.00 or -0.00 (the numbers can be any in that form) for any that have a + symbol I need to remove the +, but if it has a - symbol it needs to stay.
i need to make a new variable with the string from the old variable btut without any plus sign. I have tried a lot of different ways with no success, each thing I tried either left the + or removed the entire string. I think this should work but doesn't
foo=+12.40
bar=${foo#+}
View 4 Replies
View Related
Apr 25, 2011
how I can search within a variable and assign the results to a new variable. I'll use the following as an example -
cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=`echo ${cars} | egrep -o '<A?+|<C+'`
with the echo command I get the following output assigned to list -
A
C
C
What I'd like to get for output is -
Audi
Cadillac
Chevy
how I could do this regardless of upper/lower case letters?
View 5 Replies
View Related
May 5, 2013
I'm piping stdout from mplayer to awk, but the output stutters.
Code: Select allmplayer audiofile.m4a 2>&1 | awk -vRS="
" '$1 ~ /A:/ {print $0; fflush();}'
Instead of a steady output of lines to the terminal, output only occurs after a few seconds, between 6 or 12. This happens whether the input is from mplayer or avconv/ffmpeg. This never used to happen (a few years ago) so I wondered whether an awk update caused this to happen.
View 6 Replies
View Related
Jul 3, 2009
I have this expect process:
Code:
spawn -noecho telnet my.host.com
expect {
[code]...
View 3 Replies
View Related
Nov 24, 2010
I'm trying to write a program that will fork a series of FTP sessions. For each session, there should be separate input and output files associated with stdin and stdout/stderr.
I keep reading how I should be able to do that with dup2() in the child process before the execl(), but it's not working for me. Could someone please explain what I've done wrong? The program also has a 30-second sniper alarm for testing and killing of FTPs that go dormant for too long.
The code: (ftpmon.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
[code]....
The output:
$ ftpmon
Connected to gila-crstest.gilacorp.com (172.16.20.8).
220 (vsFTPd 2.0.1)
ftp> waitpid(): Interrupted system call
Why am I getting the ftp> prompt? If the dup2() works, shouldn't it be taking input from my script and not my terminal? In stead, it does nothing, and winds up getting killed after 30 seconds. The log file is created, but it's empty after the run.
View 3 Replies
View Related
Aug 8, 2010
I'm working on an application used for backup/archiving. That can be archiving contents on block devices, tapes, as well as regular files. The application stores data in hard packed low redundancy heaps with multiple indexes pointing out uniquely stored, (shared), fractions in the heap.
And the application supports taking and reverting to snapshot of total storage on several computers running different OS, as well as simply taking on archiving of single files. It uses hamming code diversity to defeat the disk rot, instead of using raid arrays which has proven to become pretty much useless when the arrays climb over some terabytes in size. It is intended to be a distributed CMS (content management system) for a diversity of platforms, with focus on secure storage/archiving. i have a unix shell tool that acts like gzip, cat, dd etc in being able to pipe data between applications.
Example:
dd if=/dev/sda bs=1b | gzip -cq > my.sda.raw.gz
the tool can handle different files in a struct array, like:
Code:
enum FilesOpenStatusValue {
FileIsClosed = 0,
FileIsOpen,
[code]....
Is there a better way of getting the file name of the redirected file, (respecting the fact that there may not always exist such a thing as a file name for a redirection pipe).
Should i work with inodes instead, and then take a completely different approach when porting to non-unix platforms? Why isn't there a system call like get_filename(stdin); ?
If you have any input on this, or some questions, then please don't hesitate to post in this thread. To add some offtopic to the thread - Here is a performance tip: When doing data shuffling on streams one should avoid just using some arbitrary record length, (like 512 bytes). Use stat() to get the recommended block size in stat.st_blksize and use copy buffers of that size to get optimal throughput in your programs.
View 4 Replies
View Related
Apr 28, 2010
included shell script inside c program, and i wanted to assign the value of c variable to shell variable..Can any one please suggest me how to do it?
View 8 Replies
View Related
Sep 7, 2010
I'm writing a script to execute bash commands in the PHP CLI. I would like to suppress errors from bash and write my own error message if an error occurs. So far I have this (assuming log.txt doesn't exist!):
Code:
tac log.txt 2>/dev/null
Which works as expected, tac kicks up an error but the error is suppressed, but when I use this:
Code:
tac < log.txt 2>/dev/null
I get:
Code:
bash: log.txt: No such file or directory
The tac error is suppressed but bash still gives me a dirty error.
View 2 Replies
View Related
Jul 26, 2010
I'm using libxml2 to handle/manipulate some XML files. In order to check the consistency of a XML file, I have a DTD and I'm using the xmlValidateDtd method to compute the check.
However, when an error occures during the check (for example an attribute is missing in a XML tag), then libxml2 writes the error on the stdout/stderr. For exemple:
Code:
/home/XML/FreeFour.xml:18: element CA: validity error : Element CA does not carry attribute maxlength
The method return the right result (true or false depending on the check result), but occurring errors are written on the stdout/stderr, and I actually don't want that.
View 4 Replies
View Related
Mar 17, 2011
This loop is part of a bash script which takes multiple arguments.
Code:
for ((i=1;i<=$number;++i)) ; do
offset=$(($i+5))
[code]...
View 3 Replies
View Related
Apr 20, 2010
I have a script where I want to redirect stdout to the terminal and also to a log file aswell as redirecting stderr to the same log file but not the terminal.I have the following code which I found on the net which redirects both stderr and stdout to a file and the logfile,
Code: if [ -p $PIPE1 ]
then
rm $PIPE1
[code]...
View 3 Replies
View Related
Dec 4, 2010
i want to pass variable in mysql qyery in c programming
View 1 Replies
View Related
Jun 10, 2010
I have a script that generates a bunch of output, including the expansions details provided by: set -v -xI am trying to pipe everything that is displayed to a file, in addition to displaying it on the screen. I've managed to get stderr and stdout into the file, but the expansions are only printed to the screen. Here is what I have so far:sudo -u <user> source my_job.sh |tee my_log.txt 2>&1
View 2 Replies
View Related
Jan 8, 2010
I wonder if there is a way to caputure all X keyboard events, blocking them from going to the window with keyboard focus?
View 4 Replies
View Related
Aug 13, 2010
What is the best analogue capture program please to capture Austar.
View 1 Replies
View Related
Jul 21, 2010
I wanna capture network packets from DMA ring buffer, just like netfilter. i wanna capture it from DMA, because i wanna get MAC address of I/O packets, so netfilter not included MAC address of out going packets because its on IP level and Ebtables is like that too. how i can capture network packets from DMA ring buffer.
View 9 Replies
View Related
May 31, 2011
I have a script which when invoked will generally su into being a number of different users (for those that have read other threads from me, you will know.I am building my own user based package management system).Whilst 75 - 80% of the time there will be no need to be anyone other than the original user, there are times when the root password is required. Currently this is presented to the administrator at exactly the point in the script that it is required.At times this may be more than once and it may also happen for multiple users in a row.
What would be the best / safest method (in your opinion) of capturing the password at the start of the script and then delivering it when required?I have looked at expect ( I am not at all familiar ), but on the examples dealing with passwords, that I could find, they all seem to store the password in a simple bash like variable (which does not excite me at all from a security point).I can also potentially go down the sudo road, but the issue here is that I would either have to find a list of commands that an entire group can have access to without passwords (doesn't sound safe) or I am back to square one of then requiring a password for each individual user to be entered, which if at the start would still need to be captured and saved until necessary.So as I have said, I am open to any and all (constructive) advice
View 14 Replies
View Related
Nov 25, 2008
How to write a shell script the would search for a phone no using at the end text file using sed or awk and store it in a varaible or print it.
The text file is in this form
The first line may take any form but the last line will always be Firstname Lastname:KEN:+254456789
I would like to seach for the Phone and store it in a variable and print it.
The phone no will alway be preceeded by ":+"
View 10 Replies
View Related
Oct 31, 2010
I would really like to capture the output of scp and my file's progress. Scp updates the transfer rate every 1 second, and I will like to save the transfer rate at every update. So for example, if the file transfer takes 30 seconds, I would like 30 reports of the transfer rate.
The output looks like:
Code:
file.dat 1% 3664KB 938.5KB/s 05:48
Whenever I try a simple redirect like:
Code:
scp file.dat 192.168.1.100:~/ &> output
... it does not save the rate at every update, it only shows the final rate.
If I try using typescript by starting "script" ... it's the same deal.
View 7 Replies
View Related
Mar 23, 2010
I'm creating my personal site in HTML/CSS and Perl/CGI. I have a list of links:
Quote:
<a name="link1" href="/cgi-bin/script.cgi">value1</a>
<a name="link2" href="/cgi-bin/script.cgi">value2</a>
<a name="link3" href="/cgi-bin/script.cgi">value3</a>
<a name="link4" href="/cgi-bin/script.cgi">value4</a>
<a name="link5" href="/cgi-bin/script.cgi">value5</a>
I want capture values of "value*" in <a> tags with script.cgi.
View 8 Replies
View Related
Nov 15, 2010
As we know, the data in /proc/net/dev fall into two categories -- the data received and the data transmitted. Now I want to calculate the network utilization, should the calculate be ---- (datareceived + datatransmitted - datareceivedlasttime - data transmitted last time)/ timeinterval?
View 1 Replies
View Related
Dec 9, 2010
Is there any simple example on how to take a screen cap and save it to a file?
View 11 Replies
View Related