将变量RESULTS保存到tmp文件中以便稍后调用?

时间:2015-11-15 18:09:30

标签: macos bash variables find temporary-files

运行一个脚本,找到一个装满文件的手并将他们的目录设置为变量。我使用此脚本提示用户输入的osascript对话框,然后调用与用户输入类似的文件。

一切正常。只有问题,当我运行脚本时,似乎需要10-15秒才能加载对话框。如果我在第一次之后再次运行它,它会在2-4秒内加载....

问题是:

有没有办法加快我的“查找”变量的结果?也许让它将结果存储在 tmp文件夹中,以便下次运行时,它只能搜索txt文件并将文件目录从那里放入变量...(除非他们因此,我需要做一个If-Else陈述?

#!/bin/sh

## READ LOCAL DIR FOR DIALOG BOX ICON
## CHANGE BACK-SLASH TO COLON (USE FOR VARIABLE IN OSASCRIPT)
dir=$(pwd)
dir=${dir//\//:}

abd="$(find ~/ -name "file.1")" 
cbd="$(find ~/ -name "file.2")"  
csd="$(find ~/ -name "file.3")" 
sad="$(find ~/ -name "file 4")" 


workout="$(osascript -e 'Tell application "System Events" to display dialog "Please enter your workout code:" with icon file "'$dir:appIcon.icns'" with title "Title_Here" buttons {"Cancel", "Ok"} default button 2 default answer ""' -e 'text returned of result' 2>/dev/null)"

if [ "$workout" = "ab" ] 
then 
  open "$abd"

elif [ "$workout" = "cb" ] 
then
  open "$cbd"

elif [ "$workout" = "cs" ] 
then
  open "$workout"

elif [ "$workout" = "sa" ] 
then
  open "$sad"

else
  exit     

fi

1 个答案:

答案 0 :(得分:0)

好吧,我找到了答案,似乎source完成了这项工作。为了使下面的脚本有意义,它将通过Platypus在应用程序中进行编译。

如果无论如何要压缩它,请分享。

#!/bin/sh

## READ LOCAL DIR
## CHANGE BACK-SLASH TO COLON (USE FOR VARIABLE IN OSASCRIPT)
dir=$(pwd)
dir=${dir//\//:}

## REPAIR AND RELOCATE FILE FUNCTION
## RUN FUNCTION ON FIRST LOAD AND WHEN USER INPUTS [repair]
## STORES VARIABLES IN TMP FOLDER TO BE CALLED LATER
function repaird() {    
    abd="$(find ~/ -name "FILE 1")" 
    cbd="$(find ~/ -name "FILE 2")"
    csd="$(find ~/ -name "FILE 3")"
    sad="$(find ~/ -name "FILE 4")"
    app="$(find ~/ -name "FILE.app")"  
    echo "#!/bin/sh" > /tmp/VAR_LIST.sh    ##CREATE .SH FILE TO STORE VARIABLES
    echo "" >> /tmp/VAR_LIST.sh
    echo "abd='$abd'" >> /tmp/VAR_LIST.sh
    echo "cbd='$cbd'" >> /tmp/VAR_LIST.sh
    echo "csd='$csd'" >> /tmp/VAR_LIST.sh
    echo "sad='$sad'" >> /tmp/VAR_LIST.sh
    cd /tmp
    chmod +x VAR_LIST.sh; }                

## CHECK IF VAR_LIST FILE EXIST
## IF NOT THEN PROCEED TO FIND FILES AND SAVE VARIABLES TO VAR_LIST

if ls /tmp/VAR_LIST.sh* 1> /dev/null 2>&1; then
    source /tmp/VAR_LIST.sh         ## USE SOURCE TO CALL OUR VAR_LIST 
else
    repaird
fi

## USE OSASCRIPT TO CALL DIALOG BOX WITH USER INPUT    

WORKOUT="$(osascript -e 'Tell application "System Events" to display dialog "Please enter your workout code:" & "\n[ab] = Ab" & "\n[cb] = Cb" & "\n[cs] = Cs" & "\n[sa] = Sa" & "\n[repair] = Repair Directory" with icon file "'$dir:appIcon.icns'" with title "MY_TITLE" buttons {"Cancel", "Ok"} default button 2 default answer ""' -e 'text returned of result' 2>/dev/null)"

## SEE IF USERS INPUT MATCHES ANY OF THE OPTIONS BELOW
## IF NOT THEN EXIT SCRIPT

if [ "$WORKOUT" = "ab" ] 
then 
  open "$abd"; afplay ohyeah.mp3

elif [ "$WORKOUT" = "cb" ] 
then
  open "$cbd"; afplay ohyeah.mp3

elif [ "$WORKOUT" = "cs" ] 
then
  open "$csd"; afplay ohyeah.mp3

elif [ "$WORKOUT" = "sa" ] 
then
  open "$sad"; afplay ohyeah.mp3

elif [ "$WORKOUT" = "repair" ]       ## REPAIR WILL DO THE FOLLOWING
then
  rm /tmp/VAR_LIST.sh             ## RM CURRENT VAR_LIST
  repaird                        ## RUN FUNCTION <REPAIRD>
  sleep 5; open "$app" &         ## DELAY 5 SECONDS THEN OPEN SCRIPT AS BACKGROUND
  close "$app"                   ## CLOSE SCRIPT  
else                             ## SCRIPT OPENS AFTER 5 SECONDS WHICH PROMPTS
  exit                           ## USER WITH SAME 

fi