LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2014, 07:43 PM   #1
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Rep: Reputation: Disabled
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 "`

Last edited by Lucien Lachance; 08-01-2014 at 08:37 PM.
 
Old 08-01-2014, 09:45 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
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
 
Old 08-01-2014, 09:57 PM   #3
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
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)
 
Old 08-01-2014, 10:47 PM   #4
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,622

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
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
 
Old 08-01-2014, 11:03 PM   #5
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply

Tags
bash, free bsd, linux, ruby


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Execute a shell script when clicking on a certain file extension antlarac Linux - Newbie 2 01-30-2013 09:55 AM
[SOLVED] search all the file extension in the system using shell script rajhans Linux - Newbie 1 03-31-2012 11:57 AM
[SOLVED] Add a file extension to a bunch of file names taylorkh Linux - Newbie 10 10-21-2010 01:57 PM
shell script - add file to archive ? xconspirisist Linux - Software 2 03-20-2004 03:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration