LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2014, 04:39 PM   #1
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245

Rep: Reputation: 16
problem creating an array from a function in bash


I have an array and a function which list all files in a directory and initializes the array; however, that method is not working. Aparently b/c the variable is local to the function. How can I make it global, or what would be the correct way to pass it to the function without using return

Code:
list_files() {
 files=($(ls | grep txt 2>/dev/null))
 if [ $? -ne 0 ]; then
  echo "error...."
  exit 1
}

files=""
list_files
by the way, I know this will work just fine
Code:
files=($(ls | grep txt 2>/dev/null))
I wanted to use a function which output an error in case of an empty directory
 
Old 03-20-2014, 08:43 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
I have an array and a function which list all files in a directory and initializes the array; however, that method is not working. Aparently b/c the variable is local to the function. How can I make it global, or what would be the correct way to pass it to the function without using return
Please explain what you are getting and how it differs from what you want?
Also, if you place set -xv in your script it will show you what the interpreter is doing.

Lastly, there is no need for grep and ls should not be used for parsing (see here).
Just use simple globbing:
Code:
files=( *.txt )
 
1 members found this post helpful.
Old 03-21-2014, 09:17 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by grail View Post
Just use simple globbing:
Code:
files=( *.txt )
+1

And use shopt -s failglob to get an error if there are no .txt files.
 
1 members found this post helpful.
  


Reply



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
[SOLVED] bash how pass array as one of several parameters to a function porphyry5 Programming 5 06-14-2011 09:11 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
[SOLVED] Problem with getting array from function (bash) DrLove73 Programming 19 09-25-2009 07:07 AM
passing array and variable to function in bash script ajaypitroda Programming 2 07-07-2009 11:10 PM
Bash array to function rejeep Programming 5 11-27-2007 02:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 AM.

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
Open Source Consulting | Domain Registration