DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 13th August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default my 1st sh script

This is still unfinished


Code:
#!/bin/sh

RenameFile()
{
  rm -f $1/*jpg
  rm -f $1/Thumbs.db
  rm -f $1/*.ini

  for fileName in $1/*
  do
    newFileName=$(echo "$fileName" | tr ' ' _)

    if [ "$fileName" != "$newFileName" ]
      then
        mv -f "$fileName" "$newFileName"
    fi

    if [ "`file -b "$newFileName"`" = "directory" ]
      then
        RenameFile "$newFileName"
      else
        chmod uog-x "$newFileName"
    fi

  done
}


RenameFile `pwd`
Code:
$ cd /home/share/music

$ mkplaylist.sh
chmod: /home/share/music/mp3/151/Code_Of_Tha_Street_[2003]/*: No such file or directory
chmod: /home/share/music/mp3/213/The_Hard_Way_[2004]/*: No such file or directory
chmod: /home/share/music/mp3/2Pac/2Pacalypse_Now_[1991]/*: No such file or directory
chmod: /home/share/music/mp3/2Pac/All_Eyez_On_Me_[1996]/*: No such file or directory
...
The top level files aren't renamed, and x flag ain't removed from permissions.
And i don't seem to get it why.

This script is supposed to rename all subdirectories and files in given directory so that they don't have spaces, remove windows crap files, and x attribute (which i have because i copied files from NTFS/FAT whatever)

Later i will make it also build playlists for my mplayer (which i run in slave mode), but that's another story....


plz, help me, i'm just starting to learn sh, and this is my 1st script that has more than 4 lines
Reply With Quote
  #2   (View Single Post)  
Old 13th August 2008
J65nko J65nko is online now
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,154
Thanked 182 Times in 149 Posts
Default

Code:
chmod: /home/share/music/mp3/151/Code_Of_Tha_Street_[2003]/*: No such file or directory
[2003] is a shell regular expression: It means in this case the sequence of characters :"Code_Of_Tha_Street_" followed by either a digit 2, or a digit 0, or a digit 3.

You will have to escape the "[" with a "\" to make it a literal '[' and not the start of a shell "[ ..... ]" regular expression.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #3   (View Single Post)  
Old 13th August 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,074
Thanked 198 Times in 156 Posts
Default

I would do it like this ...

Code:
#!/bin/sh

RenameFile()
{
  cd ${1}

  for f in *; do
    nf=$(echo "${f}" | tr ' ' _)

    if [ "${f}" != "${nf}" ]; then
      mv -f "${f}" "${nf}"
    fi

    if [ -d "${nf}" ]; then
      RenameFile "${nf}"
    fi
  done

  cd -
}

find . -name "*.jpg" -or -name Thumbs.db -or -name "*.ini" -delete
find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;

RenameFile .
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #4   (View Single Post)  
Old 14th August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

Quote:
Originally Posted by killasmurf View Post
Code:
$ cd /home/share/music

$ mkplaylist.sh
chmod: /home/share/music/mp3/151/Code_Of_Tha_Street_[2003]/*: No such file or directory
chmod: /home/share/music/mp3/213/The_Hard_Way_[2004]/*: No such file or directory
chmod: /home/share/music/mp3/2Pac/2Pacalypse_Now_[1991]/*: No such file or directory
chmod: /home/share/music/mp3/2Pac/All_Eyez_On_Me_[1996]/*: No such file or directory
...
maybe those directories are empty?

try this:
Code:
#!/bin/sh

RenameFile()
{
        find "$1" -depth  | while read fileName ; do
                newFileName=$(echo "$fileName" | tr ' ' _)
                if [ "$fileName" != "$newFileName" ]; then
                        mv -f "$fileName" "$newFileName"
                fi
                if [ -d "$newFileName" ] ; then
                        rm -f "$newFileName"/*jpg "$newFileName"/Thumbs.db "$newFileName"/*.ini
                else
                        chmod a-x "$newFileName"
                fi
        done
}

RenameFile "`pwd`"

Last edited by ephemera; 15th August 2008 at 07:15 AM.
Reply With Quote
  #5   (View Single Post)  
Old 15th August 2008
J65nko J65nko is online now
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,154
Thanked 182 Times in 149 Posts
Default

Ephemera, the [2003] is a shell regular expression, which will never match the string "2003".
Code:
$ mkdir test && cd test
$ pwd

/home/j65nko/test

$ for NUM in 0 1 2 3 4 ; do touch Street_${NUM} ; done
$ ls -l

total 0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_1
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_4

$ touch  Street_\[2003\]
$ ls -l

total 0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_1
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_4
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:35 Street_[2003]

$ ls -l Street_[2003]

-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3

$ ls -l Street_[023]

-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3
Got it?
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #6   (View Single Post)  
Old 15th August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

Quote:
Originally Posted by J65nko View Post
Ephemera, the [2003] is a shell regular expression, which will never match the string "2003".
Code:
$ mkdir test && cd test
$ pwd

/home/j65nko/test

$ for NUM in 0 1 2 3 4 ; do touch Street_${NUM} ; done
$ ls -l

total 0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_1
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_4

$ touch  Street_\[2003\]
$ ls -l

total 0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_1
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_4
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:35 Street_[2003]

$ ls -l Street_[2003]

-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3

$ ls -l Street_[023]

-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_0
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_2
-rw-r--r--  1 j65nko  j65nko  0 Aug 15 03:34 Street_3
Got it?
Please rest assured I didn't miss your post.

$ ls -l "Street_[2003]"

notice that it works with the quotes (as it does in the script).

now, with regards to the errors OP got:
Code:
chmod: /home/share/music/mp3/151/Code_Of_Tha_Street_[2003]/*: No such file or directory
try this:

# empty directory
$ mkdir "Street [2003]"

# OP's script will do
$ chmod uog-x "Street [2003]/*"

Last edited by ephemera; 15th August 2008 at 09:15 AM.
Reply With Quote
  #7   (View Single Post)  
Old 15th August 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,074
Thanked 198 Times in 156 Posts
Default

Quote:
notice that it works with the quotes (as it does in the script).
KillaSmurf's script doesn't use quotes:

Code:
  for fileName in $1/*
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #8   (View Single Post)  
Old 15th August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

Quote:
Originally Posted by Carpetsmoker View Post
KillaSmurf's script doesn't use quotes:

Code:
  for fileName in $1/*
I said that the dir Code_Of_Tha_Street_[2003] is probably empty.

Ok, now i will present my line of reasoning:

First note that $1/* in the for statement correctly globs the dir.
Run the following to convince yourself:
$ mkdir test
$ touch "test/Street [2003]"
$ export d=test
$ for f in $d/* ; do echo "$f" ; done

Now, lets take this error as an example:
Code:
chmod: /home/share/music/mp3/151/Code_Of_Tha_Street_[2003]/*: No such file or directory
At some point the func. RenameFile() is called with the arg. "/home/share/music/mp3/151", ie. $1=/home/share/music/mp3/151.
we then enter the for loop where the dir Code Of Tha Street [2003] is converted to Code_Of_Tha_Street_[2003] (unless ofcourse those were underscores to begin).

Next, RenameFile() is called with Code_Of_Tha_Street_[2003] and the for loop
entered with $fileName="Code_Of_Tha_Street_[2003]/*"

chmod uog-x "$fileName" will now generate the given error.

I have had enough of this discussion and I will have nothing more to say on this thread.

Last edited by ephemera; 15th August 2008 at 04:38 PM.
Reply With Quote
  #9   (View Single Post)  
Old 15th August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default

Sorry that i haven't replied to any of your post, yet
We (ppl who live in my region) have some important problems here, where i live.... that we (some volunteers try to resolve), we're collecting signatures against local project...

so i ain't got time to fallow this thread yet...
Will read everything in few days... when this is over
Reply With Quote
Old 17th August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default

Code:
#!/bin/sh

RenameFile()
{
  rm -f $1/*jpg
  rm -f $1/Thumbs.db
  rm -f $1/*.ini

  for fileName in $1/*
  do
    newFileName=$(echo "$fileName" | tr ' []' '_()')
echo "     $newFileName"

    if [ "$fileName" != "$newFileName" ]
      then
        mv -f "$fileName" "$newFileName"
    fi

    if [ "`file -b "$newFileName"`" = "directory" ]
      then
        RenameFile "$newFileName"
      else
        chmod uog-x "$newFileName"
    fi

  done
}


RenameFile `pwd`
OK, lines in red fixes my problems (so it seams)
Not the best solution, but it'll help me avoid problems later

I know it looks dirty atm, i will fix it

Thanks for replies


EDIT: fixed ugly code, more later


Edit2:

Code:
#!/bin/sh

RenameFile()
{


  for fileName in $1/*
  do
    newFileName=$(echo "$fileName" | tr ' []' '_()')
echo "  "$newFileName

    if [ "$fileName" != "$newFileName" ]
      then
        mv -f "$fileName" "$newFileName"
    fi

    if [ -d "$newFileName" ]
      then
        RenameFile "$newFileName"
      else
        chmod uog-x "$newFileName"
    fi

  done
}

find `pwd` -name "*.jpg" -delete
find `pwd` -name Thumbs.db -delete
find `pwd` -name "*.ini" -delete

RenameFile `pwd`
Now this increased performance VERY much

Last edited by graudeejs; 17th August 2008 at 03:53 PM.
Reply With Quote
Old 17th August 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,074
Thanked 198 Times in 156 Posts
Default

Why use `pwd` and not just .?
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
Old 17th August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default



he he he
Me noob scripter. lol
Reply With Quote
Old 18th August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default

Quote:
Originally Posted by Carpetsmoker View Post
Why use `pwd` and not just .?
OK, i found good reason for that....
. gives only relative paths
But if i want to use mplayer in slave mode and control it with menus...., then absolute path is way better to be used


So here are my 2 scripts
rmwinshit.sh
mkplaylists.sh

They work fine, but i'd be glad if someone could give me some suggestions, as this was ugly and hard way to make it work.... and i bet there is a better way..

mkplaylists.sh is supposed to read subdirectories in given folder and output playlists in /home/share/music/playlsts/
might be problems if you start script and have less than 2 subdirs

rmwinshit.sh removes all/most crap that you get when you get when you copy files from ntfs/msdosfs to ufs/ext2fs and friends
It also replaces some problematic characters:
Code:
space ==> _
[ ==> (
] ==> )

Last edited by graudeejs; 18th August 2008 at 10:33 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Crontab won't run script Petrocelli08 FreeBSD General 4 7th March 2009 04:19 AM
Backup script(s)? giddyupman General software and network 2 3rd January 2009 02:06 PM
Handy X11 script Gabe_G23 Guides 6 25th October 2008 05:08 PM
Automation Script ninjatux FreeBSD General 2 24th October 2008 04:16 PM
Shell Script. bsdnewbie999 Programming 21 15th July 2008 07:54 AM


All times are GMT. The time now is 09:50 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick