ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
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
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.