LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   passing filenames in current directory as arguments to other function (https://www.linuxquestions.org/questions/linux-newbie-8/passing-filenames-in-current-directory-as-arguments-to-other-function-949426/)

samasat 06-09-2012 05:11 PM

passing filenames in current directory as arguments to other function
 
Hello,

I want to list files with certain extension in current directory and then pass each one of them as input argument to another function in the script (preferably after some string trimming operations in future). I wrote following test script:
Code:

#!/bin/bash

# Test to list files with certain extension and pass them as arguments to separate function

extn=".sh"

var=`find . -name \*$extn`
echo echoing var $var
echo "Starting main action now ..."

myPrintf(){
printf 'Filename#%02d is: %s\n' "$1" "$2"
FirstFour=$2[1:4]
printf 'First four letters: %s\n' "$FirstFour"
}

runDiffMatSim(){
        printf 'Found total %d %s files in current directory:\nThose Are: %s\n' "$#" "$extn" "$*"
        myPrintf "1" "$1"
        myPrintf "2" "$2"

        for((kk=1 ; kk<=$# ; kk++ ))
        do
        myPrintf "$kk" "${$kk}"
        done
}

FileList=`find . -name \*$extn`
runDiffMatSim "$FileList"

However, output is not as expected. I want to use for loop as given in runDiffMatSim() above code to carry out actions similar to those described by myPrintf() function calls just prior to for loop. echo statement in the beginning confirms that FileList variable is storing all the desired filenames appropriately. However, I am getting error as given below:
Code:

[ssr@localhost Aim01]$ ./test2.sh
echoing var ./test.sh ./Aim01.sh ./test2.sh ./Aim01-U038.sh
Starting main action now ...
Found total 1 .sh files in current directory:
Those Are: ./test.sh
./Aim01.sh
./test2.sh
./Aim01-U038.sh
Filename#01 is: ./test.sh
./Aim01.sh
./test2.sh
./Aim01-U038.sh
First four letters: ./test.sh
./Aim01.sh
./test2.sh
./Aim01-U038.sh[1:4]
Filename#02 is:
First four letters: [1:4]
./test2.sh: line 23: ${$kk}: bad substitution
[ssr@localhost Aim01]$

Can someone help me:
1) to point out mistake causing for loop "bad substitution" error
2) to be able to pass each filename individually as argument to myPrintf() function ?
3) suggest reference for trimming file extension from name or picking up just one word e.g. U*** to give U038 above fourth file (unlike here, that U*** pattern is present in all files with specified extension in list ) ?

Thanks,

allend 06-09-2012 10:40 PM

To iterate through the files with a common extension in the current directory you can use a construction like:
Code:

for Filename in *.sh; do
done

There are various parameter expansion functions that you can use to manipulate the filenames e.g. to print the first four characters of the filename you can use:
Code:

echo "First four letters: "${Filename:0:4}
Putting this together with a counter
Code:

#!/bin/bash

for Filename in *.sh; do
  echo "File is $Filename";
  echo "First four letters: ${Filename:0:4}";
  (( Count++ ));
done
echo "Found total of $Count .sh files in current directory"

It should be trivial for you to call a custom function using the original filename or a filename manipulated using parameter expansions.

David the H. 06-10-2012 10:37 AM

In addition:

Code:

FileList=`find . -name \*$extn`
Single, scalar variables should not be used to store lists of things like filenames. That's what arrays are there for.


For trivial, single directory level matches, you can use simple globbing.

Code:

FileList=( *."$extn" )
Bash v4+ also has a new "**" globstar feature that lets you work recursively.

Code:

shopt -s globstar

FileList=( **/*."$extn" )

Note though that I've found it to still have major problems when working on certain directories, particularly my $HOME.


For more complex matches, including recursive matching, you can use find, but you need to be careful to do it in a way that handles unusual filenames correctly. This generally involves the -print0 option.

Code:

while IFS='' read -r -d '' ; do
        FileList+=( "$REPLY" )
done < <( find . -name "*.$extn" -print0 )

See these three links for more on filename reading in bash:
http://mywiki.wooledge.org/BashFAQ/001
http://mywiki.wooledge.org/BashFAQ/020
http://mywiki.wooledge.org/UsingFind



And to use the array, call it using the "@" symbol, with the whole thing in double-quotes. This ensures that each entry will be expanded as a separate string.

Code:

printf '%s\n' "${FileList[@]}"

n=1
for file in "${FileList[@]}"; do
        echo "File $(( n++ )) is: $file"
done


n=1
for index in "${!FileList[@]}"; do        #to loop by index number
        echo "File $(( n++ )) is: ${FileList[index]}"
done


BTW: $(..) is highly recommended over `..`

samasat 06-10-2012 12:07 PM

It works now ...
 
Thank you David_the_H and allend.

I could get this problem sorted out using your suggestions. :)

Here is my final code and its output -

Code:

Code:

#!/bin/bash

# Test to list files with certain extension and pass them as arguments to separate function

extn=".sh"
count=1

myPrintf(){
printf 'Filename#%02d is: %s\n' "$1" "$2"
FirstFour=${2:0:4}
printf 'First four letters: %s\n' "${FirstFour}"
}

for FileName in *$extn;
do
myPrintf "$count" "$FileName"
((count++))
done
countFinal=$(( $count - 1 ))
echo Total number of files : $countFinal


OUTPUT:
Code:

[ssr@localhost Aim01]$ ls *sh
Aim01.sh  Aim01-U038.sh  test2.sh  test3.sh  test.sh
[ssr@localhost Aim01]$ ./test3.sh
Filename#01 is: Aim01.sh
First four letters: Aim0
Filename#02 is: Aim01-U038.sh
First four letters: Aim0
Filename#03 is: test2.sh
First four letters: test
Filename#04 is: test3.sh
First four letters: test
Filename#05 is: test.sh
First four letters: test
Total number of files : 5
[ssr@localhost Aim01]$

How to use arrays may be very preliminary and basic knowledge for most of users in shell scripting but for me it was not. Thanks David_the_H for illustrating that one as well as your links about globbing.

David the H. 06-11-2012 01:21 PM

Good work!

But nah, arrays are actually one of the most overlooked of scripting features. You'd be surprised how many times posters have tried to cobble together oddball solutions when a simple array is all they really needed.

Of course, you do need to ensure that you're using a shell that supports them, like bash or ksh. POSIX-based and older shells do not.


All times are GMT -5. The time now is 11:10 PM.