View Full Version : my 1st sh script
killasmurf86
08-13-2008, 10:05 PM
This is still unfinished
#!/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`
$ 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 :)
J65nko
08-13-2008, 11:06 PM
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.
Carpetsmoker
08-13-2008, 11:20 PM
I would do it like this ...
#!/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 .
ephemera
08-14-2008, 03:13 AM
$ 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:
#!/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`"
J65nko
08-15-2008, 02:44 AM
Ephemera, the [2003] is a shell regular expression, which will never match the string "2003".$ 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? ;)
ephemera
08-15-2008, 08:13 AM
Ephemera, the [2003] is a shell regular expression, which will never match the string "2003".$ 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:
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]/*"
Carpetsmoker
08-15-2008, 03:21 PM
notice that it works with the quotes (as it does in the script).
KillaSmurf's script doesn't use quotes:
for fileName in $1/*
ephemera
08-15-2008, 05:31 PM
KillaSmurf's script doesn't use quotes:
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:
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.
killasmurf86
08-15-2008, 07:29 PM
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
killasmurf86
08-17-2008, 04:10 PM
#!/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:
#!/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
Carpetsmoker
08-17-2008, 07:26 PM
Why use `pwd` and not just .?
killasmurf86
08-17-2008, 09:11 PM
:)
he he he
Me noob scripter. lol
killasmurf86
08-18-2008, 11:25 PM
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 (http://pastebin.com/m8d42842)
mkplaylists.sh (http://pastebin.com/m4129480e)
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:
space ==> _
[ ==> (
] ==> )
vBulletin® v3.7.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.