Combine multiple shell commands to create temporary table in sqlite3 - shell

Rather than create, and then drop a table in sqlite3, I would like to create a temporary table which is destroyed when you end your session. Is it possible to combine the commands below so the temporary table is not destroyed by the semicolon ?
set databaseName to "test.db"
set tableName to "tempTable"
do shell script ("mkdir -p ~/Documents/Databases/ ; sqlite3 ~/Documents/Databases/" & databaseName & " \"create table if not exists " & tableName & "(First, Last); \"")
do shell script ("sqlite3 ~/Documents/Databases/" & databaseName & " \"insert into " & tableName & " (First, Last) values('John', 'Doe'); \"")
How would I implement this version of it?
set databaseName to "test.db"
set tableName to "tempTable"
do shell script ("mkdir -p ~/Documents/Databases/ ; sqlite3 ~/Documents/Databases/" & databaseName & " \"create temp table " & tableName & "(First, Last); \"")
do shell script ("sqlite3 ~/Documents/Databases/" & databaseName & " \"insert into " & tableName & " (First, Last) values('John', 'Doe'); \"")

SQLite accepts semicolon-separated statements itself.
do shell script ("sqlite3 ~/Documents/Databases/" & databaseName & " \" & ¬
"create temp table " & tableName & " (First, Last); " & ¬
"insert into " & tableName & " (First, Last) values('John', 'Doe'); " & ¬
" -- other stuff here --" & ¬
"select * from " & tableName & ";\"")
At some point it becomes easier to write it out as a separate script or possibly a here document than to build up a single huge do shell script, though.

Related

AppleScript that scrapes a website for data with curl function not working after some time making it

The code below used to work at scraping the trading value of a cryptocurrency website. Now it prints the name of the crypto and the time it was read but no value. Is it because the curl function isn't working anymore? Any help is appreciated.
The code uses a curl function to call the value of the crypto every 5 seconds. I got the code from a helpful stack overflow user and it worked well when I implemented it but it doesn't return the monetary value anymore. It creates a log file to the desktop and writes in the background as an application.
property eGrepBitcoinPrice : "priceValue\">\\$\\d{2},\\d{3}.\\d{2}"
property eGrepLitecoinPrice : "priceValue\">\\$\\d{2}.\\d{2}"
property eGrepDogecoinPrice : "priceValue\">\\$\\d{1}.\\d{5}"
property currentBitcoinPrice : missing value
property currentLitecoinPrice : missing value
property currentDogecoinPrice : missing value
property logToTextFile : missing value
on run -- Executed Only Once.. When This Script Applet Is Launched
activate
set logToTextFile to (display dialog ¬
"Enable Quick Log Mode?" buttons {"No", "Yes"} ¬
default button 2 with title "Log Mode")
if button returned of logToTextFile = "Yes" then
my logCommands's beginLog()
getPrices()
else
getPrices()
return {currentBitcoinPrice, currentDogecoinPrice, currentLitecoinPrice}
end if
end run
on idle
getPrices()
try
if button returned of logToTextFile = "Yes" then my logCommands's writeToLog()
on error errMsg number errNum
my logCommands's writeToLog()
end try
(* within this idle handler is where you will place
The bulk of your additional code. All of your Excel
Code Goes Here*)
return 5 -- In Seconds, How Often To Run Code In This Idle Handler
end idle
---------- PLACE ALL ADDITIONAL HANDLERS BENEATH THIS LINE ----------
on getPrices()
set currentBitcoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/bitcoin/markets/' " & ¬
"| grep -Eo " & quoted form of eGrepBitcoinPrice & " | cut -c 14-"
set currentLitecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/litecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepLitecoinPrice & " | cut -c 14-"
set currentDogecoinPrice to do shell script ¬
"curl --no-keepalive 'https://coinmarketcap.com/currencies/dogecoin/' " & ¬
"| grep -Eo " & quoted form of eGrepDogecoinPrice & " | cut -c 14-"
end getPrices
on quit -- Executed Only When The Script Quits
if button returned of logToTextFile = "Yes" then my logCommands's endLog()
continue quit -- Allows The Script To Quit
end quit
script logCommands
property pathToPriceLog : POSIX path of (path to desktop as text) & "Price Log.txt"
on beginLog()
set startTime to ("Start Time... " & (current date) as text) & ¬
" Price Scanning At 5 Minute Intervals"
do shell script "echo " & startTime & " >> " & ¬
quoted form of pathToPriceLog
end beginLog
on writeToLog()
do shell script "echo " & "Bitcoin:" & quoted form of currentBitcoinPrice & ¬
" Dogecoin:" & quoted form of currentDogecoinPrice & ¬
" Litecoin:" & quoted form of currentLitecoinPrice & ¬
" " & quoted form of (time string of (current date)) & ¬
" >> " & quoted form of pathToPriceLog
end writeToLog
on endLog()
set endTime to quoted form of "End Time... " & (current date) as text
do shell script "echo " & endTime & " >> " & ¬
quoted form of pathToPriceLog
do shell script "echo " & " " & " >> " & ¬
quoted form of pathToPriceLog
end endLog
end script

Trying to get AppleScript to create a proper zip file

Script needs to do the same as a right click > "Compress" in the Finder does.
tell application "Finder"
set myDir to choose folder
set myParentDir to the parent of myDir as alias
set myParentPath to POSIX path of myParentDir
set myDirName to the name of myDir
set myZipName to "'" & myDirName & ".zip'"
set myDirName to the quoted form of myDirName
set myParentPath to the quoted form of myParentPath
set myShellScript to "cd " & myParentPath & " | zip -r " & myZipName & " " & myDirName
tell current application
do shell script myShellScript
end tell
end tell
if I copy the string saved in myShellScript and run it in Terminal it works perfectly but when run from the script editor it fails with the following:
error " zip warning: name not matched: IDA template Folder
zip error: Nothing to do! (try: zip -r IDA template Folder.zip . -i IDA template Folder)" number 12
Any suggestions?
You need to change the pipe | in myShellScript into a newline \n, so:
set myShellScript to "cd " & myParentPath & "\n zip -r " & myZipName & " " & myDirName
NB: The alternative - and the way it will show in Script Editor when it's compiled - is simply to type a literal line feed:
set myShellScript to "cd " & myParentPath & "
zip -r " & myZipName & " " & myDirName
You should replace the | with && - that way the zip command will only run if the cd command succeeds. Like this:
set myShellScript to "cd " & myParentPath & " && zip -r " & myZipName & " " & myDirName

using VB6 executing a batch passing two arguments, two variables seperated by a space

I am attempting to execute a batch file with two arguments from vb6.
Shell ("c:\tftp\Createplkstart.txt.bat " & strMsg & " " & cfgSaved & switchIP)
however it seems to drop the second and third arguments.
If I do a msgbox :
MsgBox "\c:\tftp\Createplkstart.txt.bat " & strstart & " " & cfgSaved
it returns two lines with the space " ".
thanks for any advice

OSX 10.6.8 Xcode 4.2 Change Created and Modified Dates

I've been working on an Applescript to change the creation and modification dates of a particular file via (do shell script). The first script with a manually input date works fine, but the second script which uses the (current date command) with an appended time, throws an error code, even though the output from both scripts appears to be the same. Any help would be most appreciated.
Script #1
resetFile()
on resetFile()
do shell script "SetFile -d '02/18/2016 00:01:00' ~/Desktop/Test.txt"
do shell script "SetFile -m '02/18/2016 00:01:00' ~/Desktop/Test.txt"
end resetFile
Script #2
resetFile()
on resetFile()
set {year:y, month:m, day:d} to (current date)
set {year:y, month:m, day:d} to result
if (d * 1) < 10 then
if (m * 1) < 10 then
set dateTime to quoted form of ("0" & m * 1 & "/" & "0" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
else
set dateTime to quoted form of (m * 1 & "/" & "0" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
end if
else
if (m * 1) < 10 then
set dateTime to quoted form of ("0" & m * 1 & "/" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
else
set dateTime to quoted form of (m * 1 & "/" & d * 1 & "/" & y * 1 & " " & "00:01:00") as string
end if
end if
display dialog dateTime
do shell script "SetFile -d dateTime ~/Desktop/Test.txt"
do shell script "SetFile -m dateTime ~/Desktop/Test.txt"
end resetFile
This is a simpler version to create the date string. It uses a handler to add leading zeros if needed:
resetFile()
on resetFile()
set {year:y, month:m, day:d} to (current date)
set dateTime to quoted form of (pad(m as integer) & "/" & pad(d) & "/" & y & space & "00:01:00")
display dialog dateTime
do shell script "SetFile -d " & dateTime & " ~/Desktop/Test.txt"
do shell script "SetFile -m " & dateTime & " ~/Desktop/Test.txt"
end resetFile
on pad(v)
return text -2 thru -1 of ((v + 100) as text)
end pad

Call command program that requires user input

Using VBScript (run through cscript), how can I call another command program (pscp in this case), which also requires user input, etc.?
The purpose is that the initial VBScript will gather the parameters (password, user, etc.), then the pscp command
pscp -r -pw password copyFromPath user#host:copyToPath
can be issued and the user will be able to see the output from the pscp command, as well as being able to input (if, for example, they gave the wrong password and are required to input it again).
I currently have:
' create the command, that calls pscp from cmd
Dim comSpec : comSpec = objShell.ExpandEnvironmentStrings("%comspec%")
Dim command : command = comspec & " /C " & """" & "pscp -r -pw " & "^""" & (Replace(pscpPassword,"""","\^""")) & "^"" " _
& "^""" & (windowsPath) & "^"" " _
& pscpUser & "#" & pscpHostName & ":" & Replace(linuxPath," ","\ ") & """"
Dim objExec : Set objExec = objShell.Exec(command)
An alternative I came up with for generating command was:
Dim command : command = "pscp -r -pw " & Chr(34) & Replace(pscpPassword,"""","\""") & Chr(34) & " " _
& Chr(34) & windowsPath & Chr(34) & " " _
& pscpUser & "#" & pscpHostName & ":" & Replace(linuxPath," ","\ ")
But neither of these allow me to interact with pscp once it's called.
Edit
The fact that I'm calling pscp is almost irrelevant. I could be calling any program which asks for user input and displays things to stdout and stderr
I put all the commands together into a script (separated with &), then run them all after the loop. I append "& pause" to keep the window open (see comments under question for full details)

Resources