Extracting data from a shell script

Hi there, i’m new here, and hoping someone can help with this
Please dont ask why i’m trying to do it, it’s just for a mini project

I’m trying to write a script that will execute the command:

df -kl /

and read the 7 characters after “/dev” and add them into a text file at a particular point located by the bold text as shown below

The big criteria here is that at no point is the terminal application to be opened, because if that was part of it, a simple copy and paste instruction would make it easy. I’m trying to do this for those that dont want to have anything to do with the terminal but still have access to its strengths

My first problem is how to get the code to run as an application because at the moment as it is

do shell script "df -kl /"

It runs direct from applescript but if i save as application it just runs and does nothing, whereas if i run from applescript it displays in a window. Before i get into the searching of the result i want to make sure that it runs as an app.

I am not very good with applescript and could also do with some pointers in writing whatever is the best search and extract command, whether it be find/grep/awk or some kind of perl script. Also based on what applescript can achieve so that i can figure out the best way to do it.

Particularly interested in whether it’s best to add the text file data to the applescript with a variable and create the text file from that rather than add the variable into an already created text file. Like i say, i’m not too hot on my scripting, and any help/suggestions would be much appreciated

Many Thanks in Advance

try this in Applescript to get the value of the mounted disk

set my_results to do shell script "df -lk / | grep dev | awk '{print $1}' | awk -F '/' '{print $3}'"

It sounds to me like you’re asking about how to display the result. You just need to change your script to:


set dfResult to do shell script "df -kl /"
display dialog dfResult

It depends. AppleScript can do text processing, but if you have some complicated text munging to do, it might be worth the small hit in calling an external command-line script to process and return a result

Well, you can embed a shell script inside an AppleScript and use a UNIX-style “here document” to run it using do shell script:


do shell script "perl << EOF
print 'hi'
EOF"

Remember that if you need to include double-quotes or backslashes in your shell script, you’ll need to escape them:


do shell script "perl << EOF
print \"hi\\n\"
EOF"