do shell script to log on and execute your sql-query.
Do shell script has “/” as root, and doesn’t give you your usual environment, then you’d have to start by executing
do shell script ". $HOME/.bashrc "
but you’ll have to cd to $HOME.
Sometimes this does the trick for running things from Apple Script when it works in Terminal
do shell script "/bin/bash -c \" echo $HOME\" ; echo `pwd`"
(Then more of the original bash shell is loaded.)
We don’t usually do like that, but set it up, up front, by the usual command grouping by “;” inside the string, when you need an environment variable to have a value, like $SQLPATH, or whatever.
Now you also should see how you quote double quotes within a string, you don’t have to quote single quotes, and normal shell rules for quoting applies. When passing variables often concatenated into the string we use quoted form to retain the spaces.
like this
set mytext to "s o m e s t u f f"
set theResult to do shell script "sed 's/[[:alnum:]]/XXX/g' <<<" & quoted form of mytext
# here the variable works like it has been piped into the script like with a "<"
do shell script "echo " & quoted form of theResult & "| sed 's/X//g' |tr -s \" \""
# the variable here, almost works like a shell variable. then we use a normal pipe.
if the do shell script command executes something with an exit code unequal to 0 then the variable where you may extract any values are not set. (undefined) you can check for this with a on error try block, like this. (So it is a good idea to set any return values to null up front for instance.
set theMessage to "I'll never get out of the script"
try
set theResult to do shell script "echo " & quoted form of theMessage & " ; false "
on error
log theResult
end try
A usual way to figure out if it went ok, or not is to use a construct like this, when the command in speak doesn’t return any output of its own.
try
set theResult to do shell script "false && echo \"true\" || echo \"false\""
on error
log theResult
end try