There are good points in various answers here. Let's mix a cocktail.
First off, as mentioned in comments, you probably want to do this in a loop rather than a single command line. While xargs
is a powerful, useful command, in this case it's likely that you'll want to space things out in order to insert additional functionality in the future.
Second, as has been discussed, you need to account for column heads. Andreas quite rightly criticized other answers for leaving out the --skip-column-names
option.
Third, as it stands, your script will try to recreate directories if it's run a second time. You want to mark things as done, once directories are made.
So...
#!/bin/bash
sql="SELECT folder FROM folder WHERE created=NULL"
mysql_opts=()
mysql_opts+=(--skip-column-names) # obvious
mysql_opts+=(--host=dbhost) # database server
mysql_opts+=(--user=username) # mysql username
mysql_opts+=(--password=password) # mysql password
mysql_opts+=(-D dbname) # database name
while read name; do
if [[ ! $name =~ ^[[:alnum:]_]+$ ]]; then
echo "ERROR: invalid characters in '$name'" >&2
elif ssh remotehost "mkdir '/path/to/${name}'"; then
mysql "${mysql_opts[@]}" -e"UPDATE folder SET created=NOW() WHERE folder='$name'"
else
echo "ERROR: failed to create '$name'" >&2
fi
done < <( mysql "${mysql_opts[@]}" -e"$sql" )
When programming, always consider how much effort it will be to maintain things in the future. If options are clear and well documented, then when things break, you can fix them faster.
So, we're setting mysql's options in a bash array.
Then we're running through a loop. Input to the loop is the command after done
on the last line. This is called "Process Substitution".
Within the loop, we have an if
that first does some input validation (which could also be done in your SELECT), and if the folder name passes, runs your ssh
command. But the ssh
command is run as part of another if
. If it is successful, we mark the record as complete, with a timestamp. If it isn't successful, we get noisy about it. (Obviously, you'll need a created
column in the folder
table.)
Obviously, you can do whatever you like with your errors -- send a pager message, and email, notify your monitoring system, etc. The important thing is that you're accounting for failure, and doing something about it. (What happens if there was a temporary network failure when you were running the last mkdir
?)
Let me know if any of this is confusing. I'll be happy to clarify.