The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Root Password change bullz26 SUN Solaris 2 03-30-2008 11:09 AM
How to change ROOT password. angelofhell HP-UX 3 02-25-2008 06:44 AM
change root password duke0001 SUN Solaris 3 02-22-2007 12:10 PM
how do i change extension kswaraj Shell Programming and Scripting 2 06-28-2004 08:07 PM
How to change extension? prkwan Shell Programming and Scripting 4 11-16-2002 07:14 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #8 (permalink)  
Old 02-16-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by Astrid View Post
$ ./img2iso.sh
#: bad interpreter: No such file or directory
Perhaps a dos2unix or something similiar in Mac.

For your original script
Code:
#!/bin/bash

# Set the video directory here
VIDEO_ROOT_DIRECTORY="/Users/astrid/NFS/scripts/img2iso/";

# Check if the directory exist or not
if [ -d "$VIDEO_ROOT_DIRECTORY" ]; then
  cd $VIDEO_ROOT_DIRECTORY
else
  print "ERROR: Unable to reach directory - or it does not exist!";
  exit 1
fi

for imgfile in $(find . -type f -iname "*.img")
do
  if [ ! -f ${imgfile%.img}.iso ] ; then
    mv ${imgfile} "${imgfile%.img}.iso"
    print "[${imgfile} -> ${imgfile%.img}.iso]"
  else
    mv ${imgfile} "${imgfile%.img}-1.iso"
    print "[${imgfile} -> ${imgfile%.img}-1.iso]"
done

exit 0
Not tested tho'.
  #9 (permalink)  
Old 02-16-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
Considering the initial requirement (recursively mv .img to .iso),
with zsh:

Code:
cd /Users/astrid/NFS/scripts/img2iso/
autoload -U zmv # put it in your .zshrc
zmv -n '(**/)(*).img' '$1$2.iso'
If the result is correct
remove the n flag:

Code:
zmv '(**/)(*).img' '$1$2.iso'
Otherwise:

Code:
find /Users/astrid/NFS/scripts/img2iso -name '*.img' -exec sh -c 'f="${1%.img}.iso"
[ ! -f "$f" ]&&mv "$1" "$f"' - {} \;

Last edited by radoulov; 02-16-2008 at 09:09 AM..
  #10 (permalink)  
Old 02-16-2008
Astrid Astrid is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
With all this help I know I will get it going!
Thank you so much for your attention, it gives a lot of inspiration. I havn't come around to finish the script yet (it's that other life with kids, laundry, office work etc.) but I'll check back when I'm finished, real soon I hope.


By the way, bash seems to be living here:

Code:
$ ls -l /bin/bash
-rwxr-xr-x   1 root  wheel  1068844 Dec 12  2006 /bin/bash

(and the same goes for zsh, nice!)
  #11 (permalink)  
Old 02-17-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
A bit optimized (without the useless sed), parametric and verbose version:

Code:
test ~ $ cat img2iso.sh
#!/bin/bash

#VIDEO_ROOT_DIRECTORY="/Users/astrid/NFS/scripts/img2iso"
VIDEO_ROOT_DIRECTORY="/home/test/video"

IN_EXT=".img"
OUT_EXT=".iso"

for infile in $(find "$VIDEO_ROOT_DIRECTORY" -name "*${IN_EXT}" -type f)
do
   echo "----------"
   echo "Processing INFILE <$infile>"
   n=0
   exit=""
   suffix=""

   while [ ! "$exit" ]
   do
      newname="${infile%${IN_EXT}}${suffix}${OUT_EXT}"
      if [ ! -f "$newname" ]
      then
         echo "NEW NAME OK! Renaming to <$newname>"
         mv "$infile" "$newname"
         exit="y"
      else
         echo "NEW NAME <$newname> already exists! Incrementing suffix"
         n=$((n+1))
         suffix="-$n"
      fi
   done
done

And the output before and after execution:

Code:
test ~ $ ls -lR /home/test/video
/home/test/video:
total 16
-rw-r--r-- 1 test test    0 Feb 17 12:00 aaa.img
-rw-r--r-- 1 test test    0 Feb 17 12:00 bbb.img
-rw-r--r-- 1 test test    0 Feb 17 12:00 bbb.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ccc-1.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ccc.img
-rw-r--r-- 1 test test    0 Feb 17 12:00 ccc.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ddd-1.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ddd.img
-rw-r--r-- 1 test test    0 Feb 17 12:00 eee.img
drwxr-xr-x 2 test test 4096 Feb 17 12:06 sub1
drwxr-xr-x 2 test test 4096 Feb 17 12:06 sub2

/home/test/video/sub1:
total 4
-rw-r--r-- 1 test test  0 Feb 17 12:06 fff.img
-rw-r--r-- 1 test test  0 Feb 17 12:06 fff.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg-1.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg-2.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg.img
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 hhh-1.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 hhh.img

/home/test/video/sub2:
total 4
-rw-r--r-- 1 test test  0 Feb 17 12:06 iii.img
-rw-r--r-- 1 test test  0 Feb 17 12:06 lll.img
-rw-r--r-- 1 test test  0 Feb 17 12:06 mmm.img



test ~ $ ./img2iso.sh
----------
Processing INFILE </home/test/video/ccc.img>
NEW NAME </home/test/video/ccc.iso> already exists! Incrementing suffix
NEW NAME </home/test/video/ccc-1.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/ccc-2.iso>
----------
Processing INFILE </home/test/video/aaa.img>
NEW NAME OK! Renaming to </home/test/video/aaa.iso>
----------
Processing INFILE </home/test/video/sub1/ggg.img>
NEW NAME </home/test/video/sub1/ggg.iso> already exists! Incrementing suffix
NEW NAME </home/test/video/sub1/ggg-1.iso> already exists! Incrementing suffix
NEW NAME </home/test/video/sub1/ggg-2.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/sub1/ggg-3.iso>
----------
Processing INFILE </home/test/video/sub1/fff.img>
NEW NAME </home/test/video/sub1/fff.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/sub1/fff-1.iso>
----------
Processing INFILE </home/test/video/sub1/hhh.img>
NEW NAME OK! Renaming to </home/test/video/sub1/hhh.iso>
----------
Processing INFILE </home/test/video/eee.img>
NEW NAME OK! Renaming to </home/test/video/eee.iso>
----------
Processing INFILE </home/test/video/sub2/mmm.img>
NEW NAME OK! Renaming to </home/test/video/sub2/mmm.iso>
----------
Processing INFILE </home/test/video/sub2/iii.img>
NEW NAME OK! Renaming to </home/test/video/sub2/iii.iso>
----------
Processing INFILE </home/test/video/sub2/lll.img>
NEW NAME OK! Renaming to </home/test/video/sub2/lll.iso>
----------
Processing INFILE </home/test/video/bbb.img>
NEW NAME </home/test/video/bbb.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/bbb-1.iso>
----------
Processing INFILE </home/test/video/ddd.img>
NEW NAME OK! Renaming to </home/test/video/ddd.iso>



test ~ $ ls -lR /home/test/video
/home/test/video:
total 16
-rw-r--r-- 1 test test    0 Feb 17 12:00 aaa.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 bbb-1.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 bbb.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ccc-1.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ccc-2.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ccc.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ddd-1.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 ddd.iso
-rw-r--r-- 1 test test    0 Feb 17 12:00 eee.iso
drwxr-xr-x 2 test test 4096 Feb 17 12:09 sub1
drwxr-xr-x 2 test test 4096 Feb 17 12:09 sub2

/home/test/video/sub1:
total 4
-rw-r--r-- 1 test test  0 Feb 17 12:06 fff-1.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 fff.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg-1.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg-2.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg-3.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 ggg.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 hhh-1.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 hhh.iso

/home/test/video/sub2:
total 4
-rw-r--r-- 1 test test  0 Feb 17 12:06 iii.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 lll.iso
-rw-r--r-- 1 test test  0 Feb 17 12:06 mmm.iso
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, linux commands, unix commands

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 On



All times are GMT -4. The time now is 08:13 PM.


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

Content Relevant URLs by vBSEO 3.2.0