Storing multiple file paths in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing multiple file paths in a variable
# 1  
Old 04-10-2013
Apple Storing multiple file paths in a variable

I am working on a script for Mac OS X that, among many other things, gets a list of all the installed Applications. I am pulling the list from the system_profiler command and formatting it using grep and awk. The problem is that I want to be able to use each result individually later in the script.

Here is an example of what I have:
Code:
system_profiler SPApplicationsDataType | grep -w /Applications* | grep -v /Applications/Utilities* | awk '{ print substr($0, index($0,$2)) }'

I get a list like this:
/Applications/Safari.app
/Applications/Mail.app
/Applications/Microsoft Office 2011/Microsoft Word.app
etc.
How can I store all these results so that I can use them individually later?
# 2  
Old 04-10-2013
Put it into an array
Code:
arr=(a b c d)
echo ${arr[0]}
a

--ahamed
# 3  
Old 04-10-2013
Apple

ahamed101,

The number of results I get back will vary from computer to computer, don't I need to know the size of the array before I can use it?

---------- Post updated at 04:06 PM ---------- Previous update was at 03:58 PM ----------

I think I figured it out, but if someone has something better I would like to know. In the echo $OUTPUT line I can do whatever I want with the information.

Code:
OLD_IFS=$IFS
IFS=$'\n'
for OUTPUT in `system_profiler SPApplicationsDataType | grep -w /Applications* | grep -v /Applications/Utilities* | awk '{ print substr($0, index($0,$2)) }'`
do
	echo $OUTPUT
done
IFS=$OLD_IFS

# 4  
Old 04-10-2013
You dont need to know the array size upfront.
Code:
arr=(a b c d)
for val in ${arr[@]}
do 
  echo $val
done

a
b
c
d

To know the array size
Code:
echo ${#arr[@]}
4

HTH

--ahamed

Last edited by ahamed101; 04-10-2013 at 09:00 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. UNIX for Beginners Questions & Answers

Storing file contents to a variable

Hi All, I was trying a shell script. I was unable to store file contents to a variable in the script. I have tried the below but unable to do it. Input = `cat /path/op.diary` Input = $(<op.diary) I am using ksh shell. I want to store the 'op.diary' file contents to the variable 'Input'... (12 Replies)
Discussion started by: am24
12 Replies

3. Shell Programming and Scripting

How to perform multiple operations on a number before storing to a variable?

(I am using bash) I have a command that will find the line number in a file by searching for a string where it exists. linenumber=$(grep -n "string" $FILENAME | cut -d : -fi) This returns the line number and removes the string. Now that I have the line number I want to subtract 4 from it and... (5 Replies)
Discussion started by: prodigious8
5 Replies

4. OS X (Apple)

File paths with spaces in a variable

Hi all my very first post so go easy on me!! I am trying to build a very simple script to list a file path with spaces in. But I can't get around this problem. My script is as follows: #!/bin/bash X="/Library/Users/Application\ Support/" LS="ls" AL="-al" $LS $AL $X The response I... (5 Replies)
Discussion started by: tillett22
5 Replies

5. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 Replies

6. Shell Programming and Scripting

storing a value from another file as a variable[solved]

Hi all, im having snags creating a variable which uses commands like cut and grep. In the instance below im simply trying to take a value from another file and assign it to a variable. When i do this it only prints the $a rather than the actual value. I know its simple but does anyone have any... (1 Reply)
Discussion started by: somersetdan
1 Replies

7. Shell Programming and Scripting

Storing lines of a file in a variable

i want to store the output of 'tail -5000 file' to a variable. If i want to access the contents of that variable, it becomes kinda difficult because when the data is stored in the variable, everything is mushed together. you dont know where a line begins or ends. so my question is, how can i... (3 Replies)
Discussion started by: SkySmart
3 Replies

8. Shell Programming and Scripting

Reading from a file and storing it in a variable

Hi folks, I'm using bash and would like to do the following. I would like to read some values from the file and store it in the variable and use it. My file is 1.txt and its contents are VERSION=5.6 UPDATE=4 I would like to read "5.6" and "4" and store it in a variable in shell... (6 Replies)
Discussion started by: scriptfriend
6 Replies

9. Shell Programming and Scripting

Storing the contents of a file in a variable

There is a file named file.txt whose contents are: +-----------------------------------+-----------+ | Variable_name | Value | +-----------------------------------+-----------+ | Aborted_clients | 0 | | Aborted_connects | 25683... (6 Replies)
Discussion started by: proactiveaditya
6 Replies

10. UNIX for Dummies Questions & Answers

Storing Multiple Values in a Variable

Hi, My code is as below: integer i=7 while ((i <= 5 )); do # test_out is a variable which contains data separated by "^". a= `echo $test_out | cut -d"^" -f$i` echo "$a" (( i = i + 1)); done From the above code, i kept $i after f so that i can capture all the data which is... (1 Reply)
Discussion started by: sandeep_1105
1 Replies
Login or Register to Ask a Question