Programming :: Bash - Dynamiquely Evaluating Variable Names

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


ADVERTISEMENT

Programming :: Bash - Expression Is Always Evaluating To True

Jun 7, 2011

This line if [[ $hamachi_reachable = true && $lan_reachable = true ]]; then is always evaluating to true. I gave two ips that are not valid, so that both variables would become false yet the whole expression is evaluating as true. With that set -x in there, I get this output.

Where am I going wrong?

Code:

View 11 Replies View Related

Programming :: Evaluating Bash Script Without Saving ?

Sep 5, 2010

I blame myself but I am lazy person. Now I am trying to learn a bit a bash scripting. But saving any script to see how it works is so boring. You know these exercises:

Code:

Now save this in a file - say helloworld.sh (haha), chmod and execute to see

Code:

Is there a way to execute text as a script? I mean

Code:

View 6 Replies View Related

General :: Concatenate Variable Names In Bash?

Apr 7, 2011

In my script, and I would like to concatenate 2 variables names, to give me the true variable.I've 3 variables X1, X2 and X3, and I invoked them inside a for loop.

Code:
#!/bin/bash
X1=HELLO

[code]....

View 4 Replies View Related

General :: SH Script Not Evaluating Variable?

Jun 21, 2010

I am trying to evaluate the variable HEADER to see if it contains "HTTP/1.1 200 OK" and if so, to do something. If not, do something else. (writing to files - see below).The variable is being set, as ALL of the URLs that I am evaluating are being put into bad_urls.txt with their response headers. I can't figure out what I'm doing wrong. I've tried modifying the line in question quite a bit, trying different things, including:

if [[ ${HEADER} =~ "HTTP/1.1 200 OK" ]]; then
if [[ ${HEADER} == "HTTP/1.1 200 OK" ]]; then
if [[ HEADER = "HTTP/1.1 200 OK" ]]; then

[code]...

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

Programming :: Php And Variable Class Names?

Sep 30, 2010

How can I handle the situation below so that the "Fatal Error" message is not shown. It would be ideal if I could supply a default class to be used. I'd prefer to not use: ini_set() to supress the errors but actually be able to "handle" the error.

Code:
<?php
class MyClass

[code]....

View 2 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 :: Bash File Names With Spaces?

Jul 24, 2010

I have a laptop that I am in through SSH. The laptop does not have an Xwindow system so I am using the program fbi to open an image on my laptop screen from my SSH connection:

fbi -T 8 picture.jpg #this opens the image on the laptops tty8 terminal

I've found that making a for loop does not work with files that contain a space in the name. Something to due with a bug that they call a "feature" that stops the first variable at the first whitespace.

Using a "while" loop is not exactly what i require either seeing as I want to be able to view each image in the directory on screen and tag it accordingly, before it jumps off to the next image, and I'm not sure how to add a pause to a while loop.

How do I make a Bash script and loop Variables handle files like "files that contain spaces.jpg"

View 5 Replies View Related

Programming :: Getting Directory Names From A Given Path In Bash?

Apr 11, 2009

I am programming in bash and really stuck finding directory names. I have a script to find all the .php files on my / partition which will return the whole path. Is there a way to print directory hierarchy with all those values leaving out the forward slashes.

View 10 Replies View Related

Programming :: Bash File Names Different Than Openbox Trash

Feb 14, 2011

Im writing my first bash script. Its function is to move files to the trash can and write a log file in the same format that the system does to allow for file restoration. The problem is that in bash, everything works fine, but in the OpenBox window session, the files are named after the source directory, not the original name. Heres the script:

Code:
#!/bin/bash
# trash - Script to move file or folder to the trash can and create a log file
##### Functions #####
err_output () # Writes error message {
echo "$0: cannot stat `$1': No such file or directory"
echo "USAGE: $0 SOURCE DEST"
exit 1 } >&2

write_log_numbered ()
# Writes log entry for numbered files {
echo "[Trash Info]" > $HOME/.local/share/Trash/info/${FILE}.$max.trashinfo
echo "Path="$PWD >> $HOME/.local/share/Trash/info/${FILE}.$max.trashinfo
echo "DeletionDate="`date "+%Y-%m-%dT%H:%M:%S"` >> $HOME/.local/share/Trash/info/${FILE}.$max.trashinfo
}

write_log_unique () # Writes log entry for unique files {
echo "[Trash Info]" > $HOME/.local/share/Trash/info/${FILE}.trashinfo
echo "Path="$PWD >> $HOME/.local/share/Trash/info/${FILE}.trashinfo
echo "DeletionDate="`date "+%Y-%m-%dT%H:%M:%S"` >> $HOME/.local/share/Trash/info/${FILE}.trashinfo
} make_paths () # Makes necessary folders {
mkdir -p ~/.local/share/Trash
mkdir -p ~/.local/share/Trash/files
mkdir -p ~/.local/share/Trash/info
}

##### Main #####
make_paths
SOURCE="$1"
DEST=$(dirname ~/.local/share/Trash/files/filename)
[ -e "$SOURCE" ] || err_output "$SOURCE"
[ -d "$DEST" ] || err_output "$DEST"
FILE="$(basename "$SOURCE")"

if [ -e "${DEST}/${FILE}" ]; then
max=0 DIR="$(pwd)" cd "$DEST"
shopt -s nullglob for backup in "${FILE}."; do
nr=${backup#${FILE}.}
if [[ "$nr" =~ ^[0-9]+$ ]]; then
if (( nr>max )); then
max="$nr" fi
fi
done
cd "$DIR"
max=$(( max + 1 ))
write_log_numbered
mv -- "$SOURCE" "${DEST}/${FILE}.$max"
else
write_log_unique
mv -- "$SOURCE" "$DEST/${FILE}"
fi

So I run the script with the test file "Junk". In bash, it moves over and its named correctly.
Code:
~/.local/share/Trash/files$ ls
file file.1 Files Files.1 Junk
The log file is also named correctly

Code:
~/.local/share/Trash/info$ ls
file.1.trashinfo Files.1.trashinfo Files.trashinfo file.trashinfo Junk.trashinfo

But, when I go to view the trash can in the file manager in Openbox, the file is called "Testing" which is the name of the source directory. However, if I go to the trashcan via its full path (going to .local/, then share/) all the files are named correctly. Whats going on here? Is there some way to get the trash can to read the correct file name?

View 4 Replies View Related

Programming :: Handle Files With Spaces In Their Names In Bash?

Apr 1, 2011

*I'm using Ubuntu 10.10

My issue is I can't handle the files with spaces in their name, I've donde the below script to print each file found inside folder and subbfolders with "find".

I would like to "ls" to each file found with its complete path and with its basename too.

Code:
files=$(find . -type f)
for each in "$files"
do
ls -l "$each" # 1rst option I've tried to list with full path
ls -l "$(/bin/echo "$each")" # 2nd option I've tried to list with full path
ls -l "$(/bin/echo $(basename "$each"))" # 1nd option I've tried to list with it basename

[Code].....

How can I list "ls -l" in both cases (with full path and with basename) when there are files with spaces in their name?

View 6 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 :: 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 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







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