LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-13-2009, 02:24 PM   #1
Arodef
Member
 
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Rep: Reputation: 17
Scripting question: Using a variable in a command


I need to create a zip file of jpg and bmp files. The zip file is named after the first file it finds which ends with .dat. Here is my script:

Code:
DAT_FILE= `find . -maxdepth 1 -iname "*.dat" | head -1 | sed 's/..\(.*\)..../\1/'`
(cd pics; find . \( -name "*.bmp" -o -name "*.jpg" \) -print  | zip ../\$DAT_FILE -@ )
BTW my sed command cut off the first two chars and last four chars since find will return the filename is the form of "./filename.dat" and I just want to extract filename.

When I run this script, it creates a zip file named ".zip". How do I fix this so the zip file is named after my dat file? Thanks for any tips!
 
Old 01-13-2009, 02:37 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
your sed command is pretty redundant... just use the basename command, don't reinvent the wheel! ;-)

why is the 2nd line 1) all on one line and 2) in parentheses? Split it out, use new lines and it should make more sense.

I think you're real problem is that you have a space after the = sign. that'll screw things up loads. also don't use `find...` for execution, use $(find....) instead, better syntax makes it easier to understand.
 
Old 01-13-2009, 02:50 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
in fact i think you actually have nothing more than...
Code:
zip $(basename $(ls -1 *.dat | head -1) .dat) pics/*.bmp pics/*.jpg
unless i've over simplified things somehow. something a bit screwy about the logic behind the head usage, but it's fairly harmless if it already works for you.

Last edited by acid_kewpie; 01-13-2009 at 02:54 PM.
 
Old 01-13-2009, 03:31 PM   #4
Arodef
Member
 
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Original Poster
Rep: Reputation: 17
Thanks for the suggestions. I forgot about basename! I need the script to be case insensitive so it has to handle finding .DAT and .dat as well as JPG/jpg and BMP/bmp. Specifying multiple or case insensitive suffixes to basename is not possible it seems. BTW using head is the only way I figure out for using just the first .dat file I get back for naming the zip.

I tried removing the space after the = and that did not fix it.

Also, I think I'll need to use find instead of just using do a shell expansion of pics/*.jpg due to argument limit.

Okay figured out how to get my initial script to work:

Code:
'
DAT_FILE=$(basename $(find . -maxdepth 1 -iname "*.dat" | head -1) | sed 's/\(.*\)..../\1/')
(cd pics; find . \( -name "*.bmp" -o -name "*.jpg" \) -print  | zip ../$DAT_FILE -@ )
When I echoed the DAT_FILE in my original script, it was blank so no wonder I was getting a file called .zip only. Using the above works, although I know it's very ugly.

Last edited by Arodef; 01-13-2009 at 03:55 PM.
 
  


Reply


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
Bash scripting - find command with variable substitution and quoting bgeddy Programming 4 07-29-2022 08:28 AM
Shell scripting question - joining variables or variable + text Xyan Linux - General 6 01-03-2009 11:54 AM
BASH scripting -- command saved as variable bioinformatics_guy Linux - Newbie 10 09-09-2008 08:24 PM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:18 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