The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to make a line BLINKING in output and also how to increase font size in output mail2sant Shell Programming and Scripting 3 04-14-2008 07:30 AM
Regarding PS1,PS2,PS3,PS4 variables. anchal_khare Shell Programming and Scripting 5 02-04-2008 04:29 AM
assigning variables from standard output whamchaxed UNIX for Dummies Questions & Answers 2 12-04-2007 07:31 PM
Assigning nawk output to variables steveje0711 Shell Programming and Scripting 6 08-19-2005 05:03 PM
variables use upper case? sed : output to the same file? gusla UNIX for Dummies Questions & Answers 1 04-05-2002 12:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-20-2008
Sciencestudent Sciencestudent is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 1
output ls in variables

Hi all,

I need some help for a command in a script with bash.

I need the output from ls in separate variables to compare them later with other files.

Example:

ls map1 gives: a.txt photo.bmp log.out gcd.sh

I would like to have that:
a1=a.txt
a2=photo.bmp
...

Thanks for the help for this noob
  #2 (permalink)  
Old 04-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
This is not at all straightforward. Could you explain more about a scenario where this could be useful? If your shell has an array variable type (bash and ksh do), perhaps you could use that (at the cost of losing classic Bourne compatibility).

Here's a stab at implementing it.

Code:
# Use plain old wildcard instead of ls, works for file names with spaces etc
set --  *
# files are now in $1 $2 $3 etc
i=1
while true; do
  # Break when no arguments remain
  case $# in 0) break;; esac
  eval a$i=\$$i
  i=`expr $i + 1`
done
How exactly this is more convenient than simply having the file names in $1 $2 $3 etc is debatable, but if you need to do it multiple times in different directories, that might be a valid use case.

Note the use of eval to get a variable name with a numeric index.

Last edited by era; 04-20-2008 at 08:23 AM.. Reason: Stab at implementing this
Closed Thread

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0