0

I am currently working on a bash script where I must download files from our mySQL database, host them somewhere different, then update the database with the new location for the image. The last portion is my problem area, creating the array full of filenames and iterating through them, replacing the file names in the database as we go.

For whatever reason I keep getting these kinds of errors:

not found/X2b6qZP.png: 1: /xxx/images/X2b6qZP.png: ?PNG /xxx/images/X2b6qZP.png: 2: /xxx/images/X2b6qZP.png: : not found /xxx/images/X2b6qZP.png: 1: /xxx/images/X2b6qZP.png: Syntax error: word unexpected (expecting ")")

files=$($DOWNLOADDIRECTORY/*)
files=$(${files[@]##*/})

# Iterate through the file names in the download directory, and assign the new values to the detail table.

for file in "${files[@]}"
    do
        mysql -h ${HOST} -u ${USER} -p${PASSWORD} ${DBNAME}  "UPDATE crm_category_detail SET detail_value = 'http://xxx.xxx.x.xxx/img/$file' WHERE detail_value LIKE '%imgur.com/$file'"
    done
2
  • $DOWNLOADDIRECTORY/* is not a command, you just want the parameter expansion of it, don't you? Commented Dec 18, 2015 at 0:18
  • @BenjaminW. correct, $DOWNLOADDIRECTORY is just the variable for /tmp/images Commented Dec 18, 2015 at 0:20

4 Answers 4

4

You are trying to execute a glob as a command. The syntax to use arrays is array=(tokens):

files=("$DOWNLOADDIRECTORY"/*)
files=("${files[@]##*/}")

You are also trying to run your script with sh instead of bash.

Do not run sh file or use #!/bin/sh. Arrays are not supported in sh.

Instead use bash file or #!/bin/bash.

Sign up to request clarification or add additional context in comments.

Comments

2

whats going on right here?

files=$($DOWNLOADDIRECTORY/*)

I dont think this is doing what you think it is doing.

According to this answer, you want to omit the first $ to get an array of files.

files=($DOWNLOADDIRECTORY/*)

I just wrote a sample script

#!/bin/sh
alist=(/*)
printf '%s\n' "${alist[@]}"

Output

/bin
/boot
/data
/dev
/dist
/etc
/home
/lib
....

4 Comments

I am trying to create an array of file names, from within that directory.
And this does exactly that, try it.
Awesome, thanks a ton. Now if I iterate through the array, and plug in $files in my mySQL text, will it use the filenames?
try it out. my suggestion would be assigning the sql text to a variable, and then using the variable in the mysql command. That way you can easily echo the query string to find possible problems. Cheers, and happy coding.
2

Your assignments are not creating arrays. You need arrayname=( values for array ) as the notation. Hence:

files=( "$DOWNLOADDIRECTORY"/* )
files=( "${files[@]##*/}" )

The first line will give you all the names in the directory specified by $DOWNLOADDIRECTORY. The second carefully removes the directory prefix. I've used spaces after ( and before ) for clarity; the shell neither requires nor objects to them. I used double quotes around the variable name and expansions to keep things sane when name do contain spaces etc.

Although it isn't immediately obvious why you might do this, its advantage over many alternatives is that it preserves spaces etc in file names.

Comments

0

You could just loop directly over the files:

for file in "$DOWNLOADDIRECTORY"/*; do
    file="${file##*/}" # or file=$(basename "$file")
    # MySQL stuff
done

Some quoting added in case of spaces in paths.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.