variable issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable issue
# 1  
Old 01-27-2011
variable issue

Hi,

I'm sure that it's a very simple issue.

this is a part of my code :
Code:
while read ligne
    do
         result=`ls -R ../FILES/|grep "."$ligne"$"`
         echo $result
    done<TYPE_EXT_FILES.txt

the echo return nothing (as my variable is empty).
I'm sure that the problem becomes from my syntaxe, because, if in the expression I replace "$ligne" by a string corresponding with my extension files, it works. The second point is that is not because ligne would be empty, because I test with an echo the return of my variable result and It isn't empty.

Please Help me... I turn a round with this problem since this morning.
thanks for your help.

regards.

Last edited by Scott; 01-27-2011 at 01:01 PM.. Reason: Code tags, please...
# 2  
Old 01-27-2011
Hi,

why don't you use find?

or, if you have bash4, try
Code:
shopt -s globstar
while read -r ext
do printf '%s\n' ../FILES/**/*.$ext
done < TYPE_EXT_FILES.txt

# 3  
Old 01-27-2011
thanks for your answer.

Your propositions don't works because I need to put the result of my query in a variable (result). After I will make an 'foreach' foreache line that returns my query (nesteed loop).

for using find in place of my expression, I'm not sure that I will have the same result.
my Algo is :
- I read the file contains the extension's list.
- for each extension, I look for all the files in my target directory
- for each ligne of my result (each files), I move it to another directory (that is the point I haven't specify).

You see...

I've make the test with find, and the issue is the same :
Code:
result=`find ../FILES/ -name "*.$ext"` #don't parse the ext variable.

thanks

Last edited by Scott; 01-27-2011 at 01:13 PM.. Reason: Code tags, please...
# 4  
Old 01-27-2011
I'm a bit wary of when people say "no really, I honestly need to cram an unlimited number of results into one single variable and it won't work any other way". This is usually just a bad programming habit, not an actual need.

Consider:

Code:
while read EXT
do
        find ../FILES/ -name "*.${EXT}" | while read FILE
        do
                mv -i "${FILE}" "/path/to/destination/folder"
        done
done < extlist.txt

This lets you process it one name at a time so you don't need to worry about shell limits.

Cramming unlimited numbers of arguments is a bad idea for many reasons:
  • What happens if you get a file named "stuff to do.ext"? Stuff it into one giant list and it'd become three files, "stuff", "to", "do.ext".
  • Many shells have surprisingly small limits on the maximum length you can fit in one shell variable. As the number of files gets large it'll break when you least expect it.
Using pipes to process names one at a time avoids these both.

Actually, you can simplify that using the -exec feature of find:

Code:
while read EXT
do
        find ../FILES/ -name "*.${EXT}" -exec mv -i '{}' "/path/to/destination/folder" ';'
done < extlist.txt

...and if you're using Linux, you can use the -t feature of mv and the + feature of find, which will let it move multiple files with one command very quickly:
Code:
while read EXT
do
        find ../FILES/ -name "*.${EXT}" -exec mv -t "/path/to/destination/folder" -i '{}' '+'
done < extlist.txt

---------- Post updated at 10:40 AM ---------- Previous update was at 10:20 AM ----------

Quote:
Originally Posted by skubann
I've make the test with find, and the issue is the same :
result=`find ../FILES/ -name "*.$ext"` don't parse the ext variable.

thanks
find works. Something else is wrong.

Have you tried one of my solutions?

Last edited by Corona688; 01-27-2011 at 12:27 PM..
# 5  
Old 01-27-2011
thanks for you answer,

I test your code, and he don't works to (I suppose for the same issue) : the parsing problem.
I do the same test. I replace ${EXT} by txt and then he return me the four results I waiting for.

otherwise, you perfectly right the way I do my code... sorry. I keep your algorithm, better than mine

regards.
# 6  
Old 01-27-2011
Quote:
Originally Posted by skubann
thanks for you answer,

I test your code, and he don't works to (I suppose for the same issue) : the parsing problem.
I think you're assuming the conclusion. I tested find like that on my system and it works fine. Try this test script (warning, it creates files and directories in the current directory!)

Code:
#!/bin/bash

# creates a file called ext.txt containing a few file extensions
cat <<EOF >ext.txt
txt
so
bar
EOF


# makes a bunch of folders
mkdir -p a/b c/d e/f

# makes a bunch of files
touch a/1.so c/2.so e/3.so
touch a/b/this.bar c/d/that.so e/f/something.so
touch a/what.bar c/the.bar e/f.bar

# Reads and finds all file extensions
while read EXT
do
        echo "Finding all files with $EXT extension:"

        find ./ -name "*.${EXT}"
        echo
done <ext.txt

Code:
 $ ./findext.sh
Finding all files with txt extension:
./ext.txt

Finding all files with so extension:
./a/1.so
./c/d/that.so
./c/2.so
./e/f/something.so
./e/3.so

Finding all files with bar extension:
./a/b/this.bar
./a/what.bar
./c/f.bar
./c/the.bar
./e/f.bar

$

Can you please show us the results of ls -R ../FILES/ | grep "ext" where "ext" is one of the extensions you're looking for. Maybe your path is a little off or something, preventing find from working.

Also show us what your extensions file looks like. Maybe it's not what we think it is.

Also post that complete loop as you have it now, maybe we can spot a mistake.

Last edited by Corona688; 01-27-2011 at 12:54 PM..
# 7  
Old 01-27-2011
this is a copy of my code :
Code:
while read EXT
    do
    echo " Extension: $EXT"    
    find ../SOURCES/ -name '*.${EXT}' | while read FILENAME
        do 
        echo test
        done
    
    done<TEST.txt

as you will see, it's the same to you. But he doesn't work.
you know, I have to go now. I will think about this night...
Tomorrow, I 'll see with a clear mind.

have a nice evening.

Moderator's Comments:
Mod Comment Please use code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Issue with variable within a loop

I have this file (1.txt): >sample TCGGCCCGJOHNTTTTGCGGGCCGCATTGTCGCCAGGCGCDOEGGGGTTTGCGATCGCCACGGGGCTGATGGTGGCGACCCGCTGCACCCGG I am using the following script to "trim" the sequence awk '{gsub(/^.*JOHN|DOE.*$/,"",$0)} 1' This is the output: >sample TTTTGCGGGCCGCATTGTCGCCAGGCGC ... (6 Replies)
Discussion started by: Xterra
6 Replies

2. UNIX for Dummies Questions & Answers

Issue with variable assignation

Hi All, My files(.csv) are created in download path. All the files files are having footer with count of rows or may be blank. When i am trying to put this below code in shell script and run it, the row_num is not getting assigned, but works correctly on command prompt. I am trying to put this... (2 Replies)
Discussion started by: abhi_123
2 Replies

3. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

4. Shell Programming and Scripting

Issue with AWK using a variable

Hi, I am doing an AWK in ksh as below with the string to search to be read from variable but for some reason there is no output. It works when I hard code it. awk 'substr($0,22,6)=="${VAR}"' XXX.txt' >YYY.txt On reading other posts I tried below option, 'substr($0,22,6)=="/"${VAR}/""' ... (3 Replies)
Discussion started by: junnujayz
3 Replies

5. UNIX for Dummies Questions & Answers

PLease HELP!!! PATH variable issue

Hello, I logged in to the unix solaris with my user name and then I again logged in with the sudo bash -l command now when I do echo $PATH It shows me => /usr/bin:/usr/local/bin:/usr/bin/usr/sbin:/usr/ucb:/usr/local/bin How do i find out where is this file located for setting the... (3 Replies)
Discussion started by: siddhans
3 Replies

6. Shell Programming and Scripting

Issue in Variable in SSH

Friends, I want to write a script. The logic follows 1. Script starts 2. SSH to Remote Machine and check whether /home/testUser dir is there or not. 3. If it is there, am assigning a value to a variable. else not 4. If the variable is set, the do the copy from remote machine to my local... (2 Replies)
Discussion started by: balamv
2 Replies

7. Shell Programming and Scripting

UNIX variable issue

Hi all, Something funny happen with this code: EXIST=`ssh batch@190.2.332.234 'if ; then echo 0; else echo 1 ; fi'` echo $EXIST Above code will display "1". The value of remotePath is /home/batch The value of fileName is sample.txt ========================================= ... (1 Reply)
Discussion started by: suigion
1 Replies

8. Shell Programming and Scripting

Variable sub-menu issue

The code im having problems with is highlighted in red, upon selecting option 2 on the main menu (highlighted green) my echo "NETWORK CONNECTIVITY" command seems to get overlooked and the resulting output is "Thank you for using the Operator Administrative Tool." being displayed. Can anyone tell me... (2 Replies)
Discussion started by: warlock129
2 Replies

9. Programming

Have issue with variable

Hi All, i am new to unix.. i have an issue with my unix script...let me explain the task that i want to make script.... i prepared script which will connect data base from my linux box using sqlplus cmd... however in that i want to use the variable like below.. select * from... (0 Replies)
Discussion started by: Shahul
0 Replies

10. Solaris

Variable Substitution Issue

#!/bin/ksh VAR_ONE=HELLO TEMP=ONE echo $VAR_${TEMP} ## Output is: ONE Hi, I want the output to echo HELLO and not ONE as the above script does. I know I am missing something with dollar substitution. Can anyone help me out ? Thanks. Cal (4 Replies)
Discussion started by: calredd
4 Replies
Login or Register to Ask a Question