LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-09-2012, 05:11 PM   #1
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Austin
Distribution: CentOS 6.5
Posts: 14

Rep: Reputation: Disabled
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,

Last edited by samasat; 06-09-2012 at 05:27 PM. Reason: added minor puctuations and details, corrections in code
 
Old 06-09-2012, 10:40 PM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,433

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

Last edited by allend; 06-09-2012 at 10:41 PM.
 
1 members found this post helpful.
Old 06-10-2012, 10:37 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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

Last edited by David the H.; 06-10-2012 at 10:41 AM. Reason: small format and wording fixes
 
1 members found this post helpful.
Old 06-10-2012, 12:07 PM   #4
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Austin
Distribution: CentOS 6.5
Posts: 14

Original Poster
Rep: Reputation: Disabled
Smile 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.

Last edited by samasat; 06-10-2012 at 12:08 PM. Reason: minor typos
 
Old 06-11-2012, 01:21 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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


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:Passing arguments to thread function in C Thodoris21 Programming 24 06-07-2012 06:08 AM
[SOLVED] [scripting] try to passing arguments to for cycle (inside a function) vomplete Linux - General 3 04-26-2011 07:01 PM
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
C++, indefinite function arguments and multiple constructor passing @_@ R00ts Programming 2 04-08-2005 03:33 PM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM


All times are GMT -5. The time now is 11:20 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