output ls in variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers output ls in variables
# 1  
Old 04-20-2008
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 Smilie
# 2  
Old 04-20-2008
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 09:23 AM.. Reason: Stab at implementing this
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store output in variables instead of files

Hi All, I have written a (bash) function which generates multiple files say file1 file2 file3 now I want to reduce the generation of these three files i.e. store the output of three files in variables, and generate the same results, in-order to avoid multiple creation of files how is that... (7 Replies)
Discussion started by: sam@sam
7 Replies

2. Shell Programming and Scripting

How to redirect the output of awk to different variables?

Hi, I search for a string "can be any" and print the fields $4,$6 in to variables. $ cat input <value ='can be any' string='many' version='123'/> <value ='xyz' string='less' version='456'/> $ cat tried_code SRG=`awk -F\' '{ print $4 }' input` VER=`awk -F\' '{ print $6 }' input` ... (4 Replies)
Discussion started by: greet_sed
4 Replies

3. Shell Programming and Scripting

How to split the sql output into two different variables

Hi, How to set as variable from sql output. Query: select aa.serial, ao.name from ann_amit aa JOIN ann_object ao on (aa.classid=ao.classid); I got two values from aa.serial and ao.name, I wanna make two different variable for aa.serial and ao.name. The value of aa.serial should be in... (2 Replies)
Discussion started by: KarthikPS
2 Replies

4. Shell Programming and Scripting

awk output to multiple variables

Hi I need to assign the ouput of a awk statement to two variables; below is a example of the txt file i have which I use awk against sample file testval,USA,loc2,testing02 testval1,GB,loc4,testing01 awk statement awk -F , '{print $2,$3}' USA loc2 GB loc4 I need a method where... (6 Replies)
Discussion started by: duckeggs01
6 Replies

5. Shell Programming and Scripting

Format output from command from variables

Hi , I have below command to that outputs from variables.. command: echo $INSTANCE $DATAB $status $TSLastBackup| awk '{printf("%-8s %-8s \t \n",$1,$2,$3,$4)}' | tee $LOGF the ouput is now: INSTANCE DATABSE BACKUP_STATUS BACKUPTIMESTAMP ------- -------- -------- ... (1 Reply)
Discussion started by: db2_usd
1 Replies

6. Shell Programming and Scripting

awk output to variables

Im trying to read the awk output to two variables. In the below code i need to store the if part output to a variable (NEW) and else part output to another variable (BAD). Pls shed some light on this snippet or if there is any other way to achieve it wud be appreciated (only condition is it needs... (7 Replies)
Discussion started by: michaelrozar17
7 Replies

7. Shell Programming and Scripting

Want to parse output for variables in Bash

Trying to finish up my script that automates some video encoding work. Situation: There is an MKV file to be transcoded. Problem: MKVINFO will give a bunch of output about an MKV file, included in that output are two lines I am interested in: | + Default duration: 41.708ms (23.976 fps... (9 Replies)
Discussion started by: randyharris
9 Replies

8. Shell Programming and Scripting

Storing awk output into variables

Hi all, Currently, i have a log file seperated by 'tab' and each record starting with a new line. i managed to retrieve the column that i'm interested in. (source_ip_address: xxx.xxx.xxx.xxx). example of awk output: '{ print $43 }' assuming the field is at column 43. 10.10.10.10... (4 Replies)
Discussion started by: faelric
4 Replies

9. UNIX for Dummies Questions & Answers

assigning variables from standard output

What am I doing wrong? I was searching for the answer to assigning variables from output. I found this simple response ls -l apply_want.m | read perms links owner group size mtime1 mtime2 mtime3 file this should allow me to echo the variables echo "$perms | $links | $owner | $group |... (2 Replies)
Discussion started by: whamchaxed
2 Replies

10. Shell Programming and Scripting

Assigning nawk output to variables

I do a lot of command line scripting to capture data from files or other command output. I've checked in a number of Unix and scripting books but for the life of me I can't find out how to asign field data from nawk output into variables that I can manipulate later. For example, reading a two... (6 Replies)
Discussion started by: steveje0711
6 Replies
Login or Register to Ask a Question