LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-28-2009, 05:45 PM   #1
deathalele
Member
 
Registered: May 2008
Location: deep dark north wales
Distribution: Zenwalk
Posts: 162

Rep: Reputation: 30
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
 
Old 03-28-2009, 06:54 PM   #2
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Rep: Reputation: 45
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 ${}.

Last edited by manwithaplan; 03-28-2009 at 06:58 PM.
 
Old 03-28-2009, 07:24 PM   #3
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Arch Linux, Mac OS X
Posts: 132

Rep: Reputation: 22
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.

Last edited by synss; 03-28-2009 at 07:26 PM.
 
Old 03-28-2009, 07:28 PM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
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]}

Last edited by Robhogg; 03-28-2009 at 07:32 PM.
 
Old 03-29-2009, 08:04 AM   #5
deathalele
Member
 
Registered: May 2008
Location: deep dark north wales
Distribution: Zenwalk
Posts: 162

Original Poster
Rep: Reputation: 30
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.
 
Old 03-29-2009, 09:31 AM   #6
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
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.

Last edited by Robhogg; 03-30-2009 at 01:34 PM. Reason: Clarity
 
Old 03-30-2009, 09:00 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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.
 
Old 03-31-2009, 05:24 PM   #8
deathalele
Member
 
Registered: May 2008
Location: deep dark north wales
Distribution: Zenwalk
Posts: 162

Original Poster
Rep: Reputation: 30
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
 
Old 03-31-2009, 05:59 PM   #9
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 04-01-2009, 01:36 AM   #10
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Arch Linux, Mac OS X
Posts: 132

Rep: Reputation: 22
Quote:
Originally Posted by deathalele View Post
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.
 
Old 04-01-2009, 01:58 AM   #11
biplabroy
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
Lightbulb

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
 
Old 04-01-2009, 10:00 AM   #12
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
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
 
Old 04-04-2009, 01:24 PM   #13
deathalele
Member
 
Registered: May 2008
Location: deep dark north wales
Distribution: Zenwalk
Posts: 162

Original Poster
Rep: Reputation: 30
The good reason for not using rsync is because i want o write a backup program.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 08:46 AM
bash script and exporting variables into a subshell spork Programming 3 10-14-2008 11:38 PM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
exporting variables under bash isn't working! dtheorem Programming 6 03-11-2004 08:35 AM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration