Programming :: Java Output Into Bash Variable?
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
ADVERTISEMENT
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
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
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
Jan 10, 2010
New to ubuntu and shell scripting in general... currently I stored some data into a text file. Right now, I would like to output the data from the text file and store it into a variable. Here's what I have so far:
READ_FILE=$(cat $FILE_NAME)
This definitely works and READ_FILE has the necessary data. However, this command will trigger an output to std output and I will see data on the screen, which is not what I want. I tried:
cat $FILE_NAME | $READ_FILE
and various other variants of this. It does not output to std output but neither does anything gets stored into $READ_FILE. I tried:
cat $FILE_NAME >> $READ_FILE
and it arrived at an error of "ambiguous redirect".
View 12 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
Apr 3, 2010
Suppose I want to account number of files beginning with abc , I can use "ls 'abc* | grep abc | wc -l", this will return me a number.
I want to store this number in a variable, say var1, so I tried
1. "ls 'abc* | grep abc | wc -l |read var1", but this didn't work as var1 has no value somehow.
2. var1='ls 'abc* | grep abc | wc -l', this just assign the entire string "ls 'abc* | grep abc | wc -l" to var1, which is not I wanted.
I don't want to store the value to a temporary file and then read the value from that file. I think there should be a direct way to get the value, but don't know how. I know in tcsh, one can just use set var1='ls 'abc* | grep abc | wc -l', but it also doesn't work in bash. Can anyone give any clue about this?
View 5 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 25, 2011
I'm trying to write a python script that will use the current user's name when interacting. Ex: when started, it should say "hello daweefolk" when I am logged in.
I've tried
Code:
username=os.system("echo $USER")
but the variable remains empty.
What is the correct code?
View 1 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
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
Jul 3, 2009
I know of 4 different ways to use a for loop:
1. for I in {1..10}; do echo $I; done|
2. for I in 1 2 3 4 5 6 7 8 9 10; do echo $I; done|
3. for I in $(seq 1 10); do echo $I; done|
4. for ((I=1; I <= 10 ; I++)); do echo $I; done
I have a script which uses the 1st form of for loop. I'm trying to modify it to use a variable instead of a static hard-coded value in the section that controls the looping.of the for loop.
I've tried all different ways of quoting and escaping the variable, and the problem is that the quoting chars and escape char are being translated and passed into the loop along with the value stored in the variable.
For example, to change the start value of 1 to whatever value I want passed in through a variable:
Change:
I have tried: {{$a}..10} and {`$a`..10}, to have the variable evaluated first.
I have tried using the eval() function.
I have tried single and double quotes and the backslash escape character.
Nothing I've tried works. It's probably a syntax error.
View 14 Replies
View Related
Feb 13, 2009
I'm setting up a machine that's going to be used to test randomly connected tape drives one at a time, and as such, I'm writing the test routine using mt in a bash script, for user-friendliness. The problem is the block device name changes on occasion as tape drives are swapped out and busses are rescanned, so I can't "hard code" a block name into the script.
I know programs like lsscsi and hwinfo will give you block device names as part of their output, but I can't seem to grep anything in such a way as to have the final output be just the block name (ie /dev/st2, or optimally 'st2'), so that I can just have the script read said output, and drop it into the necessary variable.
View 2 Replies
View Related
Mar 1, 2011
I am having all sorts of trouble trying to assign a variable within an awk script with the system command. I know there is a lot of ways around this problem, but for efficiency reasons, I would like to, within my awk script, do something like
system(x=3)
or
system(x=NR)
and, latter on the shell script which calls the awk script, use the variable $x. But nothing is passed to x. I have already tried things like
command = "x=3"
system(command)
and also used a pipeline within the system to pipe it to /bin/sh In fact tried a lot of stuff like that, using $(( )) etc etc etc I can create directories e write to files (yes, i could write to a file and read from there, but I dont think it is efficient, plus I am puzzled).
View 7 Replies
View Related
May 23, 2011
I'll try to be clear but I think I'm in a mess with my code right now, so it may appear here too
Here is a function. The part I want you to see is the 'eval' sentence.
This works.
It evaluates ${SourceTxt} (which is $1), takes away a part of it and uses it as an array to put data in.
That's alright.
Code:
Now, later, I NEED to count the number of elements in the array(s).
So, I try to do something like....
Which gives me the evaluated name ('#txtFields_email_customers[@]' e.g)
Or
Which gives me nothing....
I was wondering if I was reaching the limits of bash in this kind of capabilities, or if there is a.... bash-like solution. I.e. a good solution
View 7 Replies
View Related
Jan 28, 2010
I am fairly new to bash scripting, and I am trying to test a variable against a number range (1-3). This is what I have used so far: The user enters a value, then
while [ "$PROV" != "1" ] && [ "$PROV" != "2" ] && [ "$PROV" != "3" ] && [ $COUNTER -lt 4 ] || [ -z "$PROV" ] && [ $COUNTER -lt 4 ]; do
clear
echo
[code]....
Is there a cleaner way to do this? Something like:
while [ "$PROV" != "1-3" ]&& [ $COUNTER -lt 4 ] || [ -z "$PROV" ] && [ $COUNTER -lt 4 ]; do
View 3 Replies
View Related
Jul 7, 2011
Bash 3.1.7
Code:
Code:
Code:
I think read A1 A2 makes A1, A2 string variables. Then, when A2 gets the value 01, '01' should be a string. But for some reason bash takes it as numeric. I know there are no types in bash.
View 11 Replies
View Related
Oct 28, 2010
I am trying to map the coordinates of a grid.
Code:
R=7 # number of rows (lines)
C=6 # number of columns
X=200 # initial horizontal location
Y=100 # initial vertical location
[code]....
View 4 Replies
View Related