3

So I am writing a bash script which will decrypt a file so I what the syntax of the the command to be decrypt [file.jpg] So far this is my script :

openssl enc -d -aes-256-cbc -in file.jpg > file
 echo "Please make sure you add the correct extension to 
the file."

Spent a lot time trying to achieve my goal but it doest work so I would like some help. Like suppose the file name is movie.mov I should be able to decrypt it using decrypt movie.mov or any other file.

EDIT: Thanks for all your answer but I found the answer which I approved to be simple and sorry I can't approve multiple answer all the answer give some new information!

nobody user
  • 141
  • 1
  • 6

4 Answers4

4

Arguments on bash script are received with $1 for first arg, $2 for second etc...

So your script should looks like

openssl enc -d -aes-256-cbc -in $1 > file

See here

jseguillon
  • 393
  • 1
  • 10
3

You can access parameters inside a bash script using the variables $1, $2, $3. $1 is the first argument, $2 the second ...

If you run

decrypt file.jpg

You can access file.jpg in your bash script with following code:

openssl enc -d -aes-256-cbc -in $1 > file
echo "Please make sure you add the correct extension to the file."
Robin
  • 303
  • 1
  • 4
  • 16
1

How do I pass arguments to bash?
( I suppose You mean '... to a bash script?'. )
You just add the argument after the command name.
Then, in your script, You may use $1 to get the first argument, $2 to get the second argument, and so on.
See here: https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html#Positional-Parameters

To solve Your problem You may:
Create a file './decrypt' with the content:
#!/bin/bash openssl enc -d -aes-256-cbc -in "$1"
Add execution rights to the file:
chmod +x ./decrypt
Then in order to decrypt the file movie.mov:
./decrypt movie.mov > movie.mov.decrypted
IMPORTANT: Remember that doing: decrypt movie.mov > movie.mov will result in an empty file! See here: bash redirect input from file back into same file
You may also want to add Your new script to path, see here: Add a bash script to path

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

$1 will be the first argument you passed to your script, $2 the second, and so on. Below is an example that takes $1 - the 1st argument and passes it to a function inside the script called decrypt_my_file. The function also use $1 as the 1st argument that was passed to it, kind of a small script inside your script

   #!/bin/bash

    decrypt_my_file () {
      local file_that_was_given="${1}"

      openssl enc -d -aes-256-cbc -in "$file_that_was_given" \
            > decrypted."$file_that_was_given"
    }

    decrypt_my_file "$1"

this should then decrypt your filename, example ABC.jpg and created a decrypted file named "decrypted.ABC.jpg"

AndyM
  • 562
  • 4
  • 22