Programming :: Variable Substitution In BASH?

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


ADVERTISEMENT

Programming :: SH - Recursive Variable Substitution

Jul 1, 2010

I have the following test code fragment:

Code:

When I try to run it 'sh' reports the following message^

Code:

You might understand what I mean with that test code so could you recommend how to achieve the required effect?

View 7 Replies View Related

Programming :: Implement Variable Substitution In Strings?

May 6, 2011

I have an interpteter that supports string literals, and the way it works is that the lexer returns the entire string as a single token, with the quotes removed and escape sequences replaced with the literal characters they represent.

I already implemented single-quote strings, they don't interpret any characters specially except for the single quote. I partially implemented double-quoted strings, they already support all the same backslash escape sequences that C does. But I would also want to add variable substitution.

The way it would work is that "${expression}" would interpret the expression (which could just be a variable name) and replace itself with the result. But I have no idea how to do this.

In case it matters, I'm using a hand-written lexer and recursive-descent parser.

View 14 Replies View Related

Programming :: Variable Substitution In Sqlite Open Db Command?

Jun 15, 2010

I have the following TCL code:

if{ [catch {sqlite3 db /path/to/db/file} result] } {
puts stderr $result
} else {
do something
}

What I want is to use a variable for the file name/path. When I put in a variable instead of the absolute path, I get an error: "missing close-brace: possible unbalanced brace in comment while executing"

This is weird because when I run the code with the fully qualified pathname, it works fine. I substitute out the pathname for a variable containing the pathname ($variable) and I get this error.

View 1 Replies View Related

Programming :: Bash - Why Is Process Substitution <() So Much Faster

Nov 25, 2010

file allids consists of 300,000 rows, each containing a 5-7 digit numeric id. file newids consists of 20,000 rows of id's. How do you explain the following timings? time: 0.07s:

Code:

diff <(sort allids) <(sort newids)
time: 1.6s:

[code].....

View 4 Replies View Related

Programming :: Bash: Functional Difference Between Process Substitution And Here String With $( )

Mar 14, 2010

Further to solved LQ thread Bash: how to populate a list of arbitrarily named files?, what is the functional difference between feeding a loop with process substitution and feeding it with a here string with embedded command substitution? ABSG pages: process substitution, here string and command substitution. This works

Code:

while IFS= read -r -d '' file
do
files+=("$file")
done < <(find $dir -type f -print0)

[code]....

View 2 Replies View Related

Programming :: Bash - Read Content Of File To Variable And Use This Variable In For Loop ?

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

General :: Sed Substitution Of Variable With A Variable?

Dec 27, 2010

I am trying to alter the character position of residue numbers above 999 in a pdb file.The following script is an attempt to:1) Get all unique pdb residue numbers (in column 5) using awk and assign it to a variable i.2) Loop through all the values in $i and if it is greater than 999, shift that number one character to the right using sed.However, the script only manages to alter the final residue numberCould anyone please advise how I can loop through all values in $i and shift it one character to the right?

#!/bin/bash
# Script to alter position of residue number in pdb file for resid above 999
i=$(awk '{print $5}' wt-test.pdb | uniq)

[code]...

View 6 Replies View Related

Programming :: Reading A Bash Variable In Bash Scripting ?

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

Programming :: Substring Extraction - Bash Gives "bad Substitution" Error

Jul 16, 2010

I'm trying to use ${VAR:0:4} substring extraction described here: tldp.org/LDP/abs/html/string-manipulation.html and it works perfectly if i issue a command in bash. But when i put it in a script file and run it, bash gives me "bad substitution" error. Does anyone know how to fix it?

View 5 Replies View Related

General :: Shell Script -- Awk Variable Substitution?

Oct 7, 2010

I have a script in which I'm reading a file line by line and I'm finding certain position value using awk index as

Code:
# 2 spaces before open parens
pos=`echo | awk -v Line="$Line" '{ print index("'"$Line"'"," (")}'`

[code]....

View 2 Replies View Related

Programming :: Sed A Variable In Bash?

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

Programming :: How To Use Bash Variable In C Code?

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

Programming :: (BASH) Keep Variable In Scope?

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

Programming :: Bash - Passing Variable To Ssh?

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

Programming :: Passing A Variable To Bc In Bash ?

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

Programming :: Setting Variable In Bash With Sed?

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

Programming :: Bash Concatenating String To Variable ?

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

Programming :: Bash: Give A Variable The Value Of A Function?

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

Programming :: BASH - If Variable -eq String Not Working ?

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

Programming :: Bash: Making A Variable Out Of Two Other Variables?

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

Programming :: Bash - To Prepend Text To Variable

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

Programming :: Bash Redirection With Exec: Specify Fd With Variable?

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

Programming :: Grep For Variable In A Bash Script?

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

Programming :: Make Bash Replace The Value Of A Variable?

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

Programming :: Retain The Eol In Bash Variable Or Ssh Output?

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

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 View Related

Programming :: Keymapping Variable For Bash Script?

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

Programming :: Using A Variable To Control A BASH For Loop

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

Programming :: Send Block Device Name To Bash Variable?

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







Copyrights 2005-15 www.BigResource.com, All rights reserved