LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-17-2009, 09:08 AM   #1
PiNPOiNT
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Rep: Reputation: 0
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
 
Old 04-17-2009, 09:18 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by PiNPOiNT View Post
Is there some special quotes or something i need to put around it?
Yes, single quotes: 'bla bla'.

(not the "backticks": `bla bla`)
 
Old 04-17-2009, 09:28 AM   #3
PiNPOiNT
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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

Last edited by PiNPOiNT; 04-17-2009 at 09:39 AM.
 
Old 04-17-2009, 12:57 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 04-17-2009, 05:28 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by PiNPOiNT View Post
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.*

Last edited by ntubski; 04-17-2009 at 05:30 PM.
 
Old 04-17-2009, 05:48 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
  


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
Shell script help: How to pass a directory path to the script? dv502 Programming 13 09-10-2008 03:07 AM
Bash script: Parse folders in path string miceagol Programming 3 03-25-2008 06:31 PM
How to pass a c variable to a bash script? daYz Programming 3 09-28-2007 08:30 AM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 01:05 PM
Path issue in Bash gd2shoe Linux - Software 2 08-28-2005 10:18 PM

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

All times are GMT -5. The time now is 02:59 PM.

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