Debian Programming :: Assign Variable In Bash When Stdout Is Updated
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
ADVERTISEMENT
Feb 27, 2011
At my wit's end I can't find anything that I understand well enough to use. This is for a Unix class, we are working with shell scripting. File1 has 5 in it and File2 has 100 in it.The teacher wants us to read the values then do the math. This is what I have so far:#!/bin/bashvar1='cat File1'var2='cat File2'var3=`echo "scale=4; $var1 / $var2" | bc`echo The final result is: $var3
View 9 Replies
View Related
Feb 12, 2011
I'm trying to execute the following command within a bash script:
Code:
tac /var/log/system.log | head -1
The script I wrote is:
[code]...
View 1 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
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
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
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
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
Mar 11, 2011
Is it possible to assign an "here documents" section to a variable in bash?I would like to assign the following to a variable:
Code:
<html>
<body>
<p>THis is a test</p>
</body>
</html>
View 4 Replies
View Related
Nov 26, 2008
I have a config file that contains:
my.config:
Code:
Now in my bash script, I want to get the output /home/user instead of $HOME once read. So far, I have managed to get the $HOME variable but I can't get it to echo the variable. All I get is the output $HOME.
Here is my parse_cmd script:
Code:
View 3 Replies
View Related
Feb 17, 2011
how to assign a local variable value to a global variable....
View 2 Replies
View Related
Mar 25, 2011
I have beat this enough and don't get what should have been a very simple thing to do. I build a variable;
Code:
CLIST=java,lua,python,php,perl,ruby,tcl
CLIST will be used by another bash script but I need to replace the commas with a space. I
[code]...
View 2 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
Feb 17, 2011
Just a simple BASH for loop to read the file path from a text file (clean.txt) echo the variable for debug purposes, and scp it to a server I have using port 50 for SSH.
I've already formatted the entries in clean.txt to handle spaces correctly, using sed replacement.
Example from the clean.txt file:
Code:
/MP3/NAS000000001/Barenaked Ladies/Barenaked Ladies - Barenaked For The Holidays/20 Auld Lang Syne.mp3
/MP3/NAS000000001/Barenaked Ladies/Barenaked Ladies - Barenaked For The Holidays/14 Deck the Stills.mp3
[Code]....
View 5 Replies
View Related
Feb 16, 2011
I want to write a c program with some shell scripts.Now For a simple C program. I am Setting a variable called val2 in bash, now I want to use bash variable val2 in C code. How do I do that?The above doesn't work (coz its spawning a different memory space and when shell script ends the variable dies with it as per my research but how do I keep them in same memory space)Also Is there any Good reference where they teach how to integrate C and Bash Together?
View 5 Replies
View Related
Jul 15, 2011
i've just started to learn about functions in Bash scripting. I'm able to set the functions and execute the commands correctly. However, if my_var is set in the first function and then later in the script in the script the 2nd function is called, it doesn't seem to remember my_var and quits (at least i suspect this is the problem).
Here's my code (it requires yad available via webupd8. org). My specific problem seems to lay in line #27 where if we view the changelog and then exit that window, it returns to the "main" function but any subsequent commands cause a crash. Is this because of the get command on line #29? It's presumably now out of scope after calling menu on line #25?
View 8 Replies
View Related
May 8, 2010
I have a file with around 1000 IP addresses in it and I need to be able to ssh into each one of them, run a single command, and then exit. I already know the ssh command I want to run and it looks like this:
Code:
shpass -p [password] ssh -p 10022 -o StrictHostKeyChecking=no root@[ip variable] 'reboot'
(I know shpass is not good to use and keys are the correct way but I don't have any other options in this scenario.) if these ip addresses were in a .csv file, by themselves with no other information, how would I create a script to do the above command to each ip until the end of the file?
View 8 Replies
View Related
Jan 14, 2009
I cannot for the life of me get this little (simple) script I wrote to work. Here is the entire script:
Code:
#!/bin/bash
ASPECT=`mediainfo $1 |grep "Display aspect ratio" |cut -d : -f 2`
HEIGHT=`echo "320 / $ASPECT" |bc`
SIZE=`echo 320x$HEIGHT`
[code]......
An input filename ($1) is fed into mediainfo, which by the use of grep and cut spits out a single number which is the aspect ratio. This is then divided by bc into 320, which gives the desired height dimension for the file that I want ffmpeg to create for me. Finally, ffmpeg runs using the calculated dimensions... Basically, it's the passing of the $ASPECT variable to bc that seems to fail. It looks like bc won't read the output from the mediainfo line... It always crashes out with:
Code:
(standard_in) 1: illegal character: ^M I've tried doing something even simpler like this to debug by just trying it to display the calculation on the screen:
Code:
#!/bin/bash
ASPECT=`mediainfo $1 |grep "Display aspect ratio" |cut -d : -f 2`
HEIGHT=`echo "320 / $ASPECT" |bc`
echo $HEIGHT
and it does the same, so it's definitely bc that won't accept the output from mediainfo.
View 4 Replies
View Related
Jun 18, 2011
I am killing myself with this, please someone come to the rescue...
Code:
#!/bin/sh
IFILE=$@
[code]...
View 7 Replies
View Related
Jan 18, 2011
I have a program that loops over each word in a sentence. I need to append a constant to the beginning and end of each word. It works up until the last word on the line.
Code:
Output:
View 4 Replies
View Related
Aug 8, 2010
I was wondering if possible in bash for a variable to take the value of a function, I mean the function returns a value and a variable will take it. example:
[Code]....
View 3 Replies
View Related
Jan 24, 2010
Here is the code:
Code:
How ever when I run this script I get the following error
Quote:
I just don't get it, I have racked my brain trying to figure out every combination of how I should write this if statement and I can't get it to work.
View 2 Replies
View Related
Mar 18, 2010
I need to find the value of:
Code:
$Namenumber
My script asks for the name you want to look up and I want it to return the value of $Namenumber
I was thinking:
Code:
number=$"$name"number
but this returns
Code:
$Namenumber
but does not actually resolve what the variable $Namenumber is equal to.
View 4 Replies
View Related
Feb 13, 2010
The output of following code is not like it's intended ...
Code:
This is the output:
Code:
Test prepending ...apple is a nice word, hour is a nice word, But of course what I want to do in the first set of commands is to prepend the word "an" to the words "apple" and "hour" in the for-loop.
View 4 Replies
View Related
Feb 21, 2010
Within a bash script, I'm attempting to redirect file descriptors with exec, e.g. exec 3>&1 1>&2; however, I'd like to do something like exec $FD>&1 1>&2, which doesn't work because bash tries to execute the value of $FD. Various placements of eval fail, as well. Is there a way around this, or am I stuck hard-coding the descriptor?
View 3 Replies
View Related
Aug 19, 2010
I've tried every combination of ' " that I have come across in similar threads on the forum but no luck.. I have 2 files
strings.txt: contains a list of numbers, 4 digits per line file.
txt: contains a lines that I want to grep for the strings.
for example:
>cat strings.txt
3214
8746
2411
[code]....
if I echo $i in the loop, I print out the contents of strings.txt If I put 3214 in place of grep "<$i>" file.txt I get "carls phone number is 3214"
View 6 Replies
View Related
Apr 18, 2009
I have a program I am writing where I have a for loop and I want to make it substitute the variable twice like:
for ((i=0;i<5;i++)) do
echo $"$i"
done
[code]...
View 3 Replies
View Related
Jan 17, 2011
I have to save the result of ssh/grep into a file to keep the eol ("/n"):
ssh $SSH_OPTIONS $USER@$NODE "cd $LOG_DIR; grep -h '$pattern' log.*" > $file
So that when I grep on the local file again later, it can be printed out with original log lines. Otherwise, the log lines will be dropped and lines becomes concatenated into a single line, e.g., if I rewrite the script in this way, echoing the $result is not a good idea..
result=`ssh $SSH_OPTIONS $USER@$NODES "cd $LOG_DIR; grep -h '$pattern' log.*"`
is there some workaround that I can save it to a variable rather than file but still keep the eol? That will simplify my script and don't need to do all those I/Os!
View 3 Replies
View Related
Feb 20, 2011
I have a bash script that calls a java class method. The method returns a string to the linux console when run independently. how can I assign the value from the java method to a variable in a bash script?running the script: java -cp /opt/my_dir/class.method [parameter]
output: my_string if added in a bash script:
read parameter
java -cp /opt/my_dir/class.method [parameter] | read the_output
echo $the_output
the above doesnt work, I also tried unsuccessfully:
the_output=java -cp /opt/my_dir/class.method [parameter]
the_output=`java -cp /opt/my_dir/class.method [parameter]`
java -cp /opt/my_dir/class.method [parameter] 2>&1
How can i get the output stored into the_output variable?
View 6 Replies
View Related
Jul 5, 2010
Is there a variable which will tell you which keyboard mapping you are using? I want to write a bash script that will change my keymapping from Dvorak keyboard to US keyboard. I cannot seem to find a variable to read from. I was going to use the "if [ ] then; statement".
View 12 Replies
View Related