Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

petisjioweelsha

macrumors member
Original poster
Nov 7, 2011
92
7
USA
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.


Code:
#!/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

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
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

macrumors member
Original poster
Nov 7, 2011
92
7
USA

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

macrumors 68040
Mar 29, 2009
3,240
629
USA
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

macrumors 6502a
Oct 10, 2011
710
403
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.

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]
 

petisjioweelsha

macrumors member
Original poster
Nov 7, 2011
92
7
USA
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

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
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 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

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
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

macrumors member
Original poster
Nov 7, 2011
92
7
USA
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.