Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google site



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-04-2009
Registered User
 

Join Date: Dec 2009
Posts: 14
mv using variables

Hi I am trying to write a script that will do a test on a file name, and then move files in batches to newly created directories. For now I am testing it by having the script search for a single file. Everything seems to work (the script does create the directories) but can't get the file to actually move! Can anyone tell me what is wrong with the indicated line? Thanks! I am on a MacBook and here is the script:

#!/bin/sh

filecount=1
mkdir $filecount

for FILE in $DSC*
do
if [ $FILE != "test.sh" ]
then
if [ $FILE = "DSC03278.JPG" ]
mv $FILE $filecount #I think my problem is here
else #bumps filecount - will later be used to skip over marker shots
let "filecount=filecount + 1"
#mkdir $filecount
fi
fi
done
Sponsored Links
  #2 (permalink)  
Old 12-04-2009
scottn's Avatar
scottn scottn is offline Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,856
What is $DSC?


Code:
for FILE in $DSC*

Perhaps you should remove the $ there?


Code:
for FILE in DSC*

Also


Code:
let "filecount=filecount + 1"

Should be


Code:
let filecount=$filecount+1

In addition, you've missed a "then" on your second "if" statement.

You should also quote $FILE when referring to it in case your filenames contain spaces.



Code:
#!/bin/sh

filecount=1
mkdir $filecount

for FILE in DSC*; do
  if [ "$FILE" = DSC03278.JPG ]; then
    mv "$FILE" $filecount
  else
    let filecount=$filecount+1
    mkdir $filecount
  fi
done

This doesn't really answer your question!

What is a "batch"?

Last edited by scottn; 12-04-2009 at 08:28 PM..
  #3 (permalink)  
Old 12-04-2009
Registered User
 

Join Date: Dec 2009
Posts: 14
scottn believe it or not - you did fix it! It was sloppy code that was tripping it up.

I am trying to run this for a group of files (the parameters for the IF will soon change once I figure out a bit of python) - so I called it a batch. The current IF was just an easy way to do a proof of concept for the rest of the code.

Out of curiosity: what if I wanted to make the if statement wild? For instance:


Code:
if [ $FILE = "DSC0328*" ]??

I know this is wrong because it will treat * as part of the string, but I can't figure out the right combo.

Thanks again!
  #4 (permalink)  
Old 12-04-2009
scottn's Avatar
scottn scottn is offline Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,856
You don't really need the "if" at all.

The for-loop will give you all the DSC* files.


Code:
$ touch DSC1 DSC2 BLAH DSC3

$ ls -l

-rw-r--r--  1 scott  staff  0 Dec  5 02:28 BLAH
-rw-r--r--  1 scott  staff  0 Dec  5 02:28 DSC1
-rw-r--r--  1 scott  staff  0 Dec  5 02:28 DSC2
-rw-r--r--  1 scott  staff  0 Dec  5 02:28 DSC3

$ for FILE in DSC*; do
>  echo $FILE
>done


DSC1
DSC2
DSC3

  #5 (permalink)  
Old 12-04-2009
Registered User
 

Join Date: Dec 2009
Posts: 14
Right! Much newbie thanks!
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert byteArray variables to HexaString variables for Linux? ritesh_163 Programming 2 08-11-2008 12:55 AM
naming variables with variables Allasso Shell Programming and Scripting 2 06-27-2008 11:45 AM
variables fwabbly UNIX for Dummies Questions & Answers 4 07-25-2007 09:59 AM
using variables in sed bishweshwar UNIX for Advanced & Expert Users 1 06-07-2007 09:47 AM
Variables gina UNIX for Dummies Questions & Answers 3 06-04-2007 10:07 AM



All times are GMT -4. The time now is 10:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0