LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script path issue - how to pass a path as a string to a variable (https://www.linuxquestions.org/questions/programming-9/bash-script-path-issue-how-to-pass-a-path-as-a-string-to-a-variable-719844/)

PiNPOiNT 04-17-2009 09:08 AM

bash script path issue - how to pass a path as a string to a variable
 
I'm trying to pass a path as a string to an array, but its evaluating it as a command instead. (sorry im new at this, forgive me :)

I want to take the literal string "/mnt/accounts/user/temp/*.jpg"
and assign it to an array{1}, but when i echo the array variable, it displays it as

pic1.jpg
pic2.jpg
pic3.jpg
etc
etc
...


I just want it to be the actual text "/mnt/accounts/user/temp/*.jpg"
which i will be combining with other text to create a longer path elsewhere in the code.

Is there some special quotes or something i need to put around it?

thx

PiN

Hko 04-17-2009 09:18 AM

Quote:

Originally Posted by PiNPOiNT (Post 3512204)
Is there some special quotes or something i need to put around it?

Yes, single quotes: 'bla bla'.

(not the "backticks": `bla bla`)

PiNPOiNT 04-17-2009 09:28 AM

Perhaps i'm combining the two strings together incorrectly, im still getting the list of all the jpg's

What is the proper way to combine to strings from 2 variables together?

eg:

string1="/mnt/accounts/user/"
string2="test"

string3=$string1$string2

expected: "/mnt/accounts/user/test"

and that works, but if i combine

string1="/mnt/accounts/user/"
string2='*.jpg'

string3=$string1$string2

i get a list of .jpg's

using .jpg works, so is there some special character you have to use if you want to print a * instead of using it as a wildcard?

just need need to append *.jpg to the end of a string

bigearsbilly 04-17-2009 12:57 PM

what are you trying to do?
you can use a for loop

Code:

for file in $.jpg
do
  echo $file
done

if you must use an array...
Code:

$ A=(*.mp3)
$ echo ${A[4]}
05_god_save_the_queen.mp3
$ echo ${A[5]}
06_problems.mp3
$ echo ${A[*]}
01_holidays_in_the_sun.mp3 02_bodies.mp3 03_no_feelings.mp3 04_liar.mp3 05_god_save_the_queen.mp3 06_problems.mp3 07_seventeen.mp3 08_anarchy_in_the_uk.mp3 09_submission.mp3 10_pretty_vacant.mp3 11_new_york.mp3 12_emi.mp3


ntubski 04-17-2009 05:28 PM

Quote:

Originally Posted by PiNPOiNT (Post 3512227)
using .jpg works, so is there some special character you have to use if you want to print a * instead of using it as a wildcard?

The trick is you need to quote when printing it:
Code:

~/tmp$ f=t.*
~/tmp$ echo $f
t.c t.sh
~/tmp$ echo "$f"
t.*


jschiwal 04-17-2009 05:48 PM

I'm not certain if you want a string: '/mnt/accounts/user/temp/*.jpg'
or an array variable:
/mnt/accounts/user/temp/picture1.jpg
/mnt/accounts/user/temp/picture2.jpg
/mnt/accounts/user/temp/picture3.jpg

If you want the latter, you can use:
pictures=(/mnt/accounts/user/temp/*.jpg)
Then each jpeg filename (including the path) will be in an element of a zero based array variable pictures. E.G. ${pictures[1]} will contain '/mnt/accounts/user/temp/picture2.jpg'.

If you want the former, either escape the asterisk \* or enclose the string in single quotes.


All times are GMT -5. The time now is 01:25 AM.