PDA

View Full Version : Will this script do what I want it to?




petisjioweelsha
Feb 11, 2012, 11:51 PM
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.


#!/bin/bash

# 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



xStep
Feb 12, 2012, 03:04 AM
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.

petisjioweelsha
Feb 12, 2012, 03:14 AM
I usually use a meaningful extension like .bash to distinguish from other files.

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

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.

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.

I don't understand what that means.

robvas
Feb 12, 2012, 11:54 AM
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

bernuli
Feb 12, 2012, 06:07 PM
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.

So, this is my first attempt at writing a shell script.
I intend to trigger it with a shortcut manager such as Keyboard Maestro, iKey or Spark.
[/CODE]

xStep
Feb 12, 2012, 07:14 PM
I don't understand what that means.

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

petisjioweelsha
Feb 12, 2012, 09:29 PM
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...

xStep
Feb 12, 2012, 11:38 PM
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...

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 (http://en.wikipedia.org/wiki/PATH_(variable)) 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.

KnightWRX
Feb 13, 2012, 05:42 AM
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.

petisjioweelsha
Feb 13, 2012, 08:59 AM
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.


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

KnightWRX
Feb 13, 2012, 10:24 AM
Yes, for now it is always the same file and the files will be indeed be hard-coded into the script.

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