Bash, Dynamic Variable Problem >.<


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash, Dynamic Variable Problem >.<
# 1  
Old 08-31-2013
Bash, Dynamic Variable Problem >.<

Hello,
I have a problem with a bash script, I've been doing recherches but i can't make it work. It is my first time with a dynamic variable and i don't understand how to write it.

Code:
#!/bin/bash
USER=BARSPIN
HOTKEYS_PATH="/home/$USER/Documents/bash/tibia/HOTKEYS"

CFG1="A"
CFG2="B"
CFG3="C"
# i have around 100 CFGX variables..

EXIT=0
if [ -e "$HOTKEYS_PATH" ]; then
    N=1
    while [ $N -lt 100 ]; do
        if [ -e "$HOTKEYS_PATH/"$CFG"$N"".cfg" ]; then
            N=$($N+1)
        else
            echo  " "$CFG"$N"".cfg is missing."
            EXIT=1
            break
        fi
    done
else
    echo " The hotkeys path is wrong"
    EXIT=1
fi
.....

Thanks Alot!!!

Last edited by barspin; 08-31-2013 at 01:04 PM..
# 2  
Old 08-31-2013
I would just use arrays. Try:
Code:
CFG[1]=A
CFG[2]=B
CFG[3]=C
...
if [ -e "$HOTKEYS_PATH/${CFG[$N]}.cfg" ]; then

# 3  
Old 08-31-2013
It doesn't works :/
# 4  
Old 08-31-2013
What does not work?
# 5  
Old 08-31-2013
Why not try it longhand first?
Code:
Last login: Sat Aug 31 17:26:40 on ttys000
AMIGA:barrywalker~> for n in {1..11}; do cpg[$n]="Number is $n..."; done
AMIGA:barrywalker~> n=2
AMIGA:barrywalker~> echo "${cpg[$n]}"
Number is 2...
AMIGA:barrywalker~> n=5
AMIGA:barrywalker~> echo "${cpg[$n]}"
Number is 5...
AMIGA:barrywalker~> n=0
AMIGA:barrywalker~> echo "${cpg[$n]}"

AMIGA:barrywalker~> # cpg[0] does not exist!
AMIGA:barrywalker~> n=123
AMIGA:barrywalker~> echo "${cpg[$n]}"

AMIGA:barrywalker~> # cpg[123] does not exist!
AMIGA:barrywalker~> n=11
AMIGA:barrywalker~> echo "${cpg[$n]}"
Number is 11...
AMIGA:barrywalker~> _

# 6  
Old 08-31-2013
I'd do it this way:

Code:
#!/bin/bash

USER=BARSPIN
HOTKEYS_PATH="/home/$USER/Documents/bash/tibia/HOTKEYS"

CFG[1]="A"
CFG[2]="B"
CFG[3]="C"
...

EXIT=0

if [[ -e $HOTKEYS_PATH ]]; then
    for (( N=1; N <= 100; ++N )); do  ## Or N <= ${#CFG[@]}
        if [[ ! -e $HOTKEYS_PATH/${CFG[N]}.cfg ]]; then
            echo  " $HOTKEYS_PATH/${CFG[N]}.cfg is missing."
            EXIT=1
            break
        fi
    done
else
    echo " The hotkeys path is wrong."
    EXIT=1
fi

...

This User Gave Thanks to konsolebox For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic variable name in bash

Hello, I'm trying to build a small script to store a command into a dynamic variable, and I have trouble assigning the variable. #!/bin/bash declare -a var1array=("value1" "value2" "value3") var1arraylength=${#var1array} for (( i=1; i<${var1arraylength}+1; i++ )); do mkdir... (7 Replies)
Discussion started by: alex2005
7 Replies

2. Shell Programming and Scripting

Problem with awk instructions and bash variable

Hi everyone, I'm trying to write a small script to automatize row data treatment. However, I got some trouble with the awk command. I want to use awk to extract a define paragraph from a text file. The first and final lines are defined externally in two variables called debut and fin. I... (2 Replies)
Discussion started by: TeaTimeSF
2 Replies

3. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

4. Shell Programming and Scripting

Bash script - ENV Variable context problem using su

Hello I have found some piece of code to verify and then run shell script with root permission from normal user. see : http://blog.mecworks.com/articles/2006/02/23/bash-scripting-tip-running-a-script-as-root I have wrote two scripts using this tips. - one to copy file from server to local... (6 Replies)
Discussion started by: jcdole
6 Replies

5. Shell Programming and Scripting

Dynamic Variable data problem

Hi, I am trying to call this variable declared globally STATE=`/opt/Mic*/bin/mstrctl -s IntelligenceServer gs | grep "state" | cut -f2 -d'<' | cut -f2 -d'>'` which extracts the status of a microstrategy server inside the loop. But once the value is assigned initially in the begining, it is not... (5 Replies)
Discussion started by: kavinmjr
5 Replies

6. Shell Programming and Scripting

Dynamic file name in variable

Hi guys. i have a requirment as below. I have a scripts which perform for loop for i in /backup/logs -- it will give all the logs file SC_RIO_RWVM_20120413064217303.LOG SC_RIO_RWXM_20120413064225493.LOG SC_RIO_RXXM_20120413064233273.LOG ... do open script.sh ---- in this file... (3 Replies)
Discussion started by: guddu_12
3 Replies

7. Shell Programming and Scripting

Help with Dynamic variable

I need some variable help TEMP1=Jane TEMP2=Sue X=1 eval USER=TEMP${X} echo $USER This gives output USER1 I would like to get Jane I have tried eval USER='TEMP${X}' eval USER="TEMP${X}" eval USER=`TEMP${X}` (3 Replies)
Discussion started by: Jotne
3 Replies

8. Shell Programming and Scripting

dynamic variable name

I found one post in another site with a solution for my problem the below solution should explain what I want. #!/bin/sh first="one" second="two" third="three" myvar="first" echo ${!myvar} But this gives error 'bad substitution' System info SunOS sundev2 5.9... (3 Replies)
Discussion started by: johnbach
3 Replies

9. Shell Programming and Scripting

Dynamic variable assignment

Hi All, I have the below scenario: A file test.cfg with three fields>> DATA1 DATA2 DATA3 In the script I need to assign each of the fields to variables. The number of fields will not be constant (this case we have three). Im trying to do something like this: NUM=1 OUT_DAT_NO=3 ... (4 Replies)
Discussion started by: deepakgang
4 Replies

10. Shell Programming and Scripting

Urgent!! Bash - problem using CD with variable

I am extracting directory path from file which is stored in a variable. while read inputline do script_name=`echo $inputline | cut -d' ' -f2` cd $script_name done < path_scripts.txt where as path_scripts contains input_process.bash ~/jobs/process/input/bin The script output says... (1 Reply)
Discussion started by: jacksam007
1 Replies
Login or Register to Ask a Question