LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   exporting variable in bash (http://www.linuxquestions.org/questions/programming-9/exporting-variable-in-bash-715176/)

deathalele 03-28-2009 05:45 PM

exporting variable in bash
 
Quote:

$COUNT=1
export TEST_$COUNT='hello'
works fine but if i do
Quote:

echo TEST_$count
it doesnt work and i get
Quote:

./backup.bash: line 28: TEST_1: command not found
help please

thanks in advance

manwithaplan 03-28-2009 06:54 PM

Try something simplistic at first.

Code:

export TEST="HELLO"

echo "$TEST"

HELLO

Or

Code:

COUNT=1

export TEST="hello $COUNT"

echo "$TEST" or you can ${TEST}

hello 1

I dont know what your trying to do ... so I just posted some simple examples.

Why do you need to declare COUNT? Whenever you echo a variable you need the "$FOO" quoted dollar sign, or using ${}.

synss 03-28-2009 07:24 PM

Code:

echo TEST_$count
You are missing the $ (dollar) prefix for TEST. Moreover, I do not think that what you are trying to do works. You can put what you are counting in one string, with a special character for separating the elements, like it is done for $PATH. Then you take them back knowing their position.

If you want more help, you need to tell more precisely what you try to achieve.

Another thing, do not "export" your variables needlessly.

Robhogg 03-28-2009 07:28 PM

If I read what you're doing aright, you're trying to create one or more environment variables with names of the pattern TEST_1, TEST_2...

Firstly, the assignment in the first line is invalid. In shell scripts, you assign to the name:

COUNT=1

export TEST_$COUNT="Hello" will then assign a value to the variable named TEST_1, which you would dereference using a $ sign:

Code:

> echo $TEST_1
Hello

However, to be able to use a variable as part of the name here, you would need to be able to dereference $COUNT first, then treat the whole as a variable. This will work, but a bit ugly.

Code:

#!/bin/bash

COUNT=1
export TEST_$COUNT="hello"

test="TEST_$COUNT" # assign the variable name to another variable

echo ${!test} #Use the value of $test as the name of the variable

Edited to add: However, synss has a point, and if you don't need to export the variable, you could use a bash array:
Code:

TEST[$count]="hello"
echo ${TEST[$count]}


deathalele 03-29-2009 08:04 AM

I'm writing a backup scripts that has two parts. the first part scans the selected folders and if the file has been modified, then it gets sent to the other half which copies each file one by one. This is so as not to delay the scanning, and also to prevent more than one file being copied at a time (I broke a pen-drive once doing that).
Here is a very cut down version of the code
Quote:

while [ 1 -eq 1 ]
do

COPY_FILE="FILES_TO_COPY_$COUNT"
echo $COPY_FILE
echo ${!COPY_FILE}
#if a file has been added copy it. and COUNT=`expr $COUNT + 1`
fi
done&


for FILE in `find $(cat $MOUNT".folders") -type f`
do
FILEDATE=`ls -ld --time-style='+startdate%y%m%denddate' $FILE| sed -n 's/^.*startdate\(.*\)enddate.*$/\1/p' `


if [ $FILEDATE -gt $BACKDATE ]
then


export FILES_TO_COPY_$COUNT2=$FILE
COUNT2=`expr $COUNT2 + 1`
export FILES_TO_COPY_$COUNT2=0
fi
done
echo ${!COPY_FILE} gives me 0, which suggests FILES_TO_COPY_$COUNT is not global.

Robhogg 03-29-2009 09:31 AM

The & operator, in my understanding, spawns a sub-shell. In this case, I think the problem is that the sub-shell is spawning before the environment variables are set, and so is not inheriting them. For instance (making things very simple) this:

Code:

count1=1
count2=1

for listed in $(ls /etc)
do
  export FILE_$count2=$listed
  count2=$(($count2 + 1))
done

while [ $count1 -le 10 ]
do
  file="FILE_$count1"
  if [ "${!file}" != "" ]
  then
      echo $file: ${!file}
      count1=$(($count1 + 1))
  fi
done &

... produces a listing of 10 files, while:

Code:

while [ $count1 -le 10 ]
do
  file="FILE_$count1"
  if [ "${!file}" != "" ]
  then
      echo $file: ${!file}
      count1=$(($count1 + 1))
  fi
done &

for listed in $(ls /etc)
do
  export FILE_$count2=$listed
  count2=$(($count2 + 1))
done

... produces no output, and has to be killed.

Could a solution be to use a temporary file? The scanning routine could append the names of the files it finds to this, and the copying routine could use wc -l to check for new lines being added:
Code:

file_len=$(wc -l $tmp_file | cut -f1 -d' ')
if [ $previous_line -lt $file_len ]
then
  file_to_copy=$(tail  -$(($file_len - $previous_line)) $tmp_file | head -1)
  previous_line=$(($previous_line + 1))
  # do copying
fi
...

Edited to add: I'm assuming there's a good reason not to use (e.g.) rsync.

chrism01 03-30-2009 09:00 PM

Actually '&' puts the cmd into the background whilst the current script continues, asynchronously ...
A sub-shell is when you use the cmd 'bash' inside a script. It only inherits exported variables, and you cannot export them back 'up' the tree.

deathalele 03-31-2009 05:24 PM

could i inverse the script and do it similar to this way
Quote:

for FILE in `find`
do
export FILE
done&

while [ 1 -eq 1]
do
echo $FILE
done
The idea of writing to a temporary file (although it should work easily) looks over complicated in terms of resources

Robhogg 03-31-2009 05:59 PM

Quote:

Originally Posted by chrism01 (Post 3493159)
Actually '&' puts the cmd into the background whilst the current script continues, asynchronously ...
A sub-shell is when you use the cmd 'bash' inside a script. It only inherits exported variables, and you cannot export them back 'up' the tree.

I was going on what it says in man bash:
Quote:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell.
Only the environment variables set before the background loop was started appeared to be inherited - those set afterwards were not, as far as I could tell.

synss 04-01-2009 01:36 AM

Quote:

Originally Posted by deathalele (Post 3494208)
The idea of writing to a temporary file (although it should work easily) looks over complicated in terms of resources

I think that should work but then, you can make it into a one liner
Code:

find . -exec echo '{}' \;
(you can background it if you like).

And use a temporary file to store the filenames, not an exported variable.

But then again, as Robhogg rightly suggested before me, you should use rsync for backups. It can do everything you want, and more.

biplabroy 04-01-2009 01:58 AM

i am a new programmer for linux. i am working in embedded system for last 15 years. i am working with ATMEL and PIC, I have compleated 50+ projects for industry and lab.

i am interested to developed kernel program as well as user application program in linux.
i found linux is the master of all os and it is very very transperent, flexcible and rugged.

now i am facing a problem.

i have create a project by KDeveloper,
the Dialog Box is working (button, textbox, lebel etc) but i want to create a chield window by which i will be able to display curve, chart, pie and other realeated graphics job.

any body can help me to draw graphics by KDeveloper

Robhogg 04-01-2009 10:00 AM

Hi biplabroy,

Welcome to the forum.

Please start a new thread for this question, with an appropriate title, rather adding it to an existing thread on another topic. You'll be more likely to get a helpful answer that way.

Yours,
Rob

deathalele 04-04-2009 01:24 PM

The good reason for not using rsync is because i want o write a backup program.


All times are GMT -5. The time now is 05:48 AM.