LinuxQuestions.org
Visit Jeremy's Blog.
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 04-28-2013, 09:26 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Rep: Reputation: 0
Question Something not working quite right in arrays/parsing arrays..


Guys,

Here is what is not working is I am unable to parse the array

Code:
N=0;
                for i in $(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print) ; do
                    input_files_array[$N]="$i"
                 #  echo "$i"
                #   echo $file_name
                    let "N= $N + 1"
                done >> ${input_files}
                for i in "${input_files_array[@]}"
                do
                file_name=${input_file_array}|cut -d'/' -f7
                echo {file_name[$i]}
                done
             fi
             exit 0
Code:
+ N=0
++ find /data/integration/ -path '*/input/*' -type f -not -name 'EAReturn*' -mmin +60 -print
find: /data/integration/file.controldir/fileftp: Permission denied
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT
+ let 'N= 0 + 1'
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT
+ let 'N= 1 + 1'
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -name "EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/BulkVehicleFileProcessor/validate/input/BVUInputFilePOL000004.txt
+ let 'N= 2 + 1'
+ for i in '"${input_files_array[@]}"'
+ file_name=
+ cut -d/ -f7
+ echo '{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}'
{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}
+ for i in '"${input_files_array[@]}"'
+ file_name=
+ cut -d/ -f7
+ echo '{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}'
{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}
+ for i in '"${input_files_array[@]}"'
+ file_name=
+ cut -d/ -f7
+ echo '{file_name[/data/integration/BulkVehicleFileProcessor/validate/input/BVUInputFilePOL000004.txt]}'
{file_name[/data/integration/BulkVehicleFileProcessor/validate/input/BVUInputFilePOL000004.txt]}
+ exit 0
I would like to understand why my file_name variable is empty when I just want the name of file? I don't see anything wrong from syntax point of view.

Please assist here.

Thank you.
 
Old 04-28-2013, 09:42 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Your line
Code:
file_name=${input_file_array}|cut -d'/' -f7
seems to have some problems.

You have here input_file_array and everywhere else input_files_array.
You are piping to cut from a left hand side that produces no output.
Maybe you meant
Code:
file_name=$(echo ${input_file_array}|cut -d'/' -f7)
or similar?
 
Old 04-28-2013, 09:54 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
2nd loop; see embedded comments
Code:
for i in "${input_files_array[@]}"
do
   # Typo in array name, and in any case I think you want $i instead,
   # and enclose in $() to get name: file_name=$(echo $i|cut -d'/' -f7)
   file_name=${input_file_array}|cut -d'/' -f7
   # echo $file_name ...
   echo {file_name[$i]}
done
I'd also use single quotes instead of backslashes in 1st loop find param; easier to read (see the output you've got).
You might also use arithmetic (( )) instead of 'let'
http://wiki.bash-hackers.org/syntax/expansion/arith

EDIT: use basename instead of cut

Last edited by chrism01; 04-28-2013 at 10:38 PM. Reason: use basename instead of cut
 
1 members found this post helpful.
Old 04-28-2013, 09:59 PM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Original Poster
Rep: Reputation: 0
Thank you for your improvement , works better but still not quite right. It just outputs same file name
 
Old 04-28-2013, 10:32 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Well I am curious about:
Code:
echo {file_name[$i]}
As file_name does not appear to be an array and $i is not an index but rather the name of a path, I am not sure I see how this would work??
 
1 members found this post helpful.
Old 04-28-2013, 10:38 PM   #6
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Original Poster
Rep: Reputation: 0
Question

@chrism01.

Many thanks for the assistance. It seems there is a problem with how we are defining a file name with cut command.

Have a look at these ones.

Code:
++ echo /data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT
++ cut -d/ -f7
+ file_name=DDDHWBC0602012112012093600.TXT <-- correct
+ echo '{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}'
{file_name[/data/integration/FIPaymentGateway/processDirectDebitDishonours/input/DDDHWBC0602012112012093600.TXT]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT
++ cut -d/ -f7
+ file_name=Asset<-- incorrect finfact file name should be 280060340997.TXT
+ echo '{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}'
{file_name[/data/integration/RTAHubGateway/inboundReceiveFiles/input/Asset/280060340997.TXT]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/temp/EAReturn20130423050714.dat
++ cut -d/ -f7
+ file_name=temp<-- incorrect , file name should be EAReturn20130423050714.dat
+ echo '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/temp/EAReturn20130423050714.dat]}'
{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/temp/EAReturn20130423050714.dat]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat
++ cut -d/ -f7
+ file_name=restore<-- incorrect filename should be EAReturn20130410015021.dat
+ echo '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat]}'
{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat]}
+ for i in '"${input_files_array[@]}"'
++ echo /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat
++ cut -d/ -f7
+ file_name=EAReturn20130425013515.dat
+ echo '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat]}'
{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat]}
SO how to get around with these ones? I am not sure if there any way to extract the last last part of the string OR with some regular expression which defines file froma string as the one with extension as .TXT or .dat or *.jpg? how should we go about this?
 
Old 04-28-2013, 10:46 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
See my late EDIT: use basename cmd instead of cut; its what its designed to do.
 
1 members found this post helpful.
Old 04-28-2013, 10:47 PM   #8
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Original Poster
Rep: Reputation: 0
Slowly and steady wins up the race.
Code:
for i in "${input_files_array[@]}"; do  file_name=basename[$i]; echo ${file_name[$i]}; done
 
Old 04-28-2013, 11:03 PM   #9
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Original Poster
Rep: Reputation: 0
Unhappy

doesn't work chrism01

Code:
                for i in "${input_files_array[@]}"
                do
                file_name=$(echo $i|basename)
                echo {file_name[$i]}
                done
Output

Code:
basename: missing operand
Try `basename --help' for more information.
+ file_name=
 
Old 04-28-2013, 11:32 PM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Original Poster
Rep: Reputation: 0
Guys,

Now I am becoming a little knowledgeable as to why are we using *two* loops when the job can be done within one loop itself ! ?
 
Old 04-28-2013, 11:54 PM   #11
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Original Poster
Rep: Reputation: 0
Unhappy

What is the issue now?

Code:
                                N=0;
                                for i in $(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -wholename "*/restore/EAReturn*" -mmin +60 -print) ; do
                                input_files_array[$N]="$i"
                             #  echo "$i"
                                #       echo $file_name
                                        let "N= $N + 1"
                                echo -e "file_name=$(basename $i)\n"
                                echo -e {file_name[$i]}<-- I am interested in array so I can use it to insert in database ! hopefully...
                                done >> ${input_files}
                     fi
But output is not that smart enough !

Code:
+ let 'N= 165 + 1'
++ basename /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat
+ echo -e 'file_name=EAReturn20130410015021.dat\n'
+ echo -e '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/restore/temp/EAReturn20130410015021.dat]}'
+ for i in '$(find ${BASE_DIRECTORY} -path \*/input/* -type f -not -wholename "*/restore/EAReturn*" -mmin +60 -print)'
+ input_files_array[$N]=/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat
+ let 'N= 166 + 1'
++ basename /data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat
+ echo -e 'file_name=EAReturn20130425013515.dat\n'
+ echo -e '{file_name[/data/integration/EnforcementGatewayVIC/notifyInfringementResponse/input/EAReturn20130425013515.dat]}' <here what i am getting is not interested in....
Probably time to give up....
 
Old 04-29-2013, 12:20 AM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
You seem confused about when you've got an array & when you don't...
If you only want the bare filenames, stored in an array arr1
Code:
N=0;
for i in $(find ${BASE_DIRECTORY} -path '*/input/*' -type f -not -wholename '*/restore/EAReturn*' -mmin +60 -print)
do
    # Store filename in new array                             
    arr1+=($(basename $i))
    (( N++ ))
done 
echo "${arr1[@]}"
NB: single quotes are safe from interpolation by the shell when using eg wildcards,
 
1 members found this post helpful.
Old 04-29-2013, 12:24 AM   #13
sysmicuser
Member
 
Registered: Mar 2010
Posts: 359

Original Poster
Rep: Reputation: 0
Yes chrism01 I am confused can you enlighten me please? I want to extract string(directory paths) and file names? What should I do. Please help me understand.

Thank you very much.
 
Old 04-29-2013, 01:19 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Well, you can see how I got the filename using basename cmd; use dirname cmd to get the path if you want it as well.
You should bookmark and READ these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Use 'man cmdname' to lookup a cmd syntax or google it.
 
1 members found this post helpful.
Old 04-29-2013, 05:37 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
I also like to use the parameter substitution method instead of basename:
Code:
file_name=${i##*/}

path_name=${i%/*}/
 
1 members found this post helpful.
  


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
help with arrays rec3g Programming 6 04-15-2008 08:04 AM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 02:39 PM
Arrays baks Programming 2 03-19-2007 01:32 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 06:40 AM
Static Arrays in php Not working ElementNine Programming 4 03-05-2004 11:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:53 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration