1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Will this script do what I want it to?

Discussion in 'Mac Programming' started by petisjioweelsha, Feb 11, 2012.

  1. macrumors member

    So, this is my first attempt at writing a shell script.
    Do I have the syntax correct?
    Do I need to do anything other than save it as a .txt file?
    I intend to trigger it with a shortcut manager such as Keyboard Maestro, iKey or Spark.

    # a script to increase the security of a particular file before it is manually uploaded to a cloud storage
    #  service by encrypting it with openssl symetric encryption.
    # remove the existing file, we don't need to keep past versions of the file.
    rm -rf /Users/myusername/path/to/folder/file
    # wait a moment
    sleep 1
    # openssl commands to encrypt the file and place it in the desired folder. The same folder that we 
    # cleared out in step one. We use a passphrase specified on the first line of password.txt
    # For our purposes it is ok to have the password stored in a plaintext file locally.
    openssl enc -aes-128-cbc -salt -in Users/myusernmae/path/to/the/filetoencrypt -out /Users/myusername/path/to/file -pass file:/Users/myusername/path/to/secret/password.txt
  2. macrumors 68000

    Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5)

    I usually use a meaningful extension like .bash to distinguish from other files.

    You'll have to set the file permissions to be executable.

    Lastly, you'll either have to execute it like ./filename from within it's folder, via the full path, or place the folder path in you PATH environment variable.
  3. macrumors member

    I did set the script file to chmod 700, but it was still opening a terminal window whenever it was run - I didn't like that.
    So, I created a one line AppleScript that points to the actual shell script file with the 'do shell script' command.
    I saved that AppleScript as an Application and it seems to run as desired.

    I don't understand what that means.
  4. macrumors 68000

    To run a script from a Terminal window you have to type the name of the script

    And since the place you have the script file saved (Documents for example) isn't in your path, you have to specify you want to run that file in your current directory, hence needing to use ./myscript
  5. macrumors regular

    save as service

    If you don't want to use a 3rd party to trigger it, try using automater to create a new "service". After saving service, then logging out and in, you can assign a global shortcut key.

  6. macrumors 68000

    robvas answered that. Don't worry about it since you came up with another solution to run it without the terminal.
  7. macrumors member

    It did run fine when double clicked, I just didn't like the fact that it left a terminal window open.

    What is a PATH?
    What is ./ ?

    Does that pertain to all scripts?

    Like I said, this one seems to worked/works fine without it...
  8. macrumors 68000

    PATH is a variable exposed to your executable. Shells have it predefined. You see it in shell scripts where depending on the scripting language, it may be lower cased if I recall right. The variable contains a list of paths in order for the executable to use to find other executables. When you type ls in a shell, the shell does the work to find the ls command by looking through each path in the PATH variable. When it finds ls, it stops looking further and executes the one it found. So my manipulating the ordered list of paths in the PATH command, you can determine which version, like your own, gets executed. This Wikipedia entry probably does a better job than I do at explaining it.

    ./ references the current directory you are set to.

    Not having seen your 'do shell script' line, I can't say how your executable got executed. My guess is you gave the full or referenced path.
  9. macrumors Pentium


    Why is everyone telling the OP how to run the script ? He says he has no problems running it.

    The question here was simple : Will this script do what the OP wants it to do.

    In this case, the OP is asking if his script will encrypt a file. To answer :

    No, it won't do anything since you're specifying a path that probably does nothing. There's some work to be done, unless your file is always the same one and you're going to hard code it.

    If you want to specify a file to the script via arguments, you'll have to use either the shift function or bash's $[0-9] built-in variables or GNU's getopt built-in or external program.

    If not, I don't see a problem with your shell script.
  10. macrumors member

    Yes, for now it is always the same file and the files will be indeed be hard-coded into the script.
  11. macrumors Pentium


    Then your script will work as intended as long as your openssl command-line works.

Share This Page