LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using Shell Injection to Add a File Extension (https://www.linuxquestions.org/questions/programming-9/using-shell-injection-to-add-a-file-extension-4175513039/)

Lucien Lachance 08-01-2014 07:43 PM

Using Shell Injection to Add a File Extension
 
How I can add the file extension inside of the shell injection without having to use ruby to add the extension? I'd prefer to keep the solution inside of the backticks.

Code:

def self.md5_sum(uri)
  `md5 -s $(basename #{uri}) | cut -d"=" -f2 | \
    tr -d "\n "` + uri[-4..-1]
end

=> "8de6a806194b000c51eee39e35b470ec.jpg"

What I came up with:

(Note the file extension could end in ".png")

Code:

`md5 -s $(basename #{uri}) | cut -d"=" -f2 | \
  awk '{ print $1, ".jpg" }' | tr -d "\n "`


NevemTeve 08-01-2014 09:45 PM

it is 'echo' or 'printf' in shell
Don't forget though that calling shell instead using the built-in tools is:

* very slow
* can be insecure
* makes the program platform-dependent
* a sure sign of laziness

Lucien Lachance 08-01-2014 09:57 PM

I learned this after running some tests on this. I'm more concerned about security than anything, so I went with:

Code:

Digest::MD5.hexdigest(File.basename(uri)) + File.extname(uri)

John VV 08-01-2014 10:47 PM

now you are not just changing any png format images and renaming them to jpg

that will not work out well

a jpg format is not a png format

Lucien Lachance 08-01-2014 11:03 PM

Correct, that was just an attempt to solve the issue. Ultimately, it's better to use the standard tool provided by Ruby which was my answer after @NevemTeve above.


All times are GMT -5. The time now is 04:17 PM.