LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Better way to do this? (https://www.linuxquestions.org/questions/linux-software-2/better-way-to-do-this-843679/)

tuxtutorials 11-11-2010 09:17 AM

Better way to do this?
 
Hello all,

Need some input from how others would solve the following issue:

I needed to download a file using wget, however the url was something like:

Code:

https://content.web.webserve.com/web/public/NULL/package-name/anotherdir/x86_64/tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm?__gda__=128942323423423423406_8d5a0asdfasdfasdf678cef05e0&ext=.rpm
and the file was being saved as such. Did some reading and figured out you can use the -O to save output to custom format. However I did not want to manually input each file name so I came up with the following non-intelligent shell script:

Code:

#!/bin/bash

URL_IN=$1
OUTPUTFILE_NAME=`echo "$URL_IN" | awk -F? '{ print $1 }' | awk -F/ '{ print $NF }'`

wget -O $OUTPUTFILE_NAME $URL_IN

exit

It works but in my opinion the awk code is non-efficient and could have probably be done in one pipe or even more efficiently. My goal here was to strip off tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm. Can someone chime in and give some pointers on a better way to do this?

Thanks

colucix 11-11-2010 09:34 AM

Code:

echo $URL_IN | awk 'gsub(/.*\/|?.*/,"")'

catkin 11-11-2010 09:38 AM

It would run faster using bash parameter expansion (because no need for creating new processes):
Code:

c@CW8:~$ buf='https://content.web.webserve.com/web/public/NULL/package-name/anotherdir/x86_64/tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm?__gda__=128942323423423423406_8d5a0asdfasdfasdf678cef05e0&ext=.rpm'
c@CW8:~$ buf=${buf%%\?*} && echo $buf
https://content.web.webserve.com/web/public/NULL/package-name/anotherdir/x86_64/tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm
c@CW8:/etc$ buf=${buf##*/} && echo $buf
tomcat5-webapps-5.5.23-0jpp.14.el5.x86_64.rpm


tuxtutorials 11-15-2010 02:20 PM

Thank you very much colucix, catkin for the input, both approaches work perfectly!


All times are GMT -5. The time now is 05:16 AM.