actually its cshell :(
Main Topics
Browse All TopicsHi Experts,
I am trying to write a shell script for converting upper case names of .cpp & .h files into lower case,
my script is checking if the file is not directory, if it was directory it will print a warning,
then it will check if the file name is not in lower case already, if it is, then it prints a warning message
and it checks also if there exists a file with same lower case name, if there was it will print a warning again,
at the end it will convert the name of the file into lower case.
could any one tell me that if this script is good enough and working , and also I thought in some places I need to quite the program but I dont know how to quite in Unix...
foreach file (*.cpp *.h) // loop for .cpp & .h file
if (-d $file )then // if the file is directory
echo 'Warning: $file is Directory' // print warning message
elseif (-f $file) then // else if it is file
set var=$file // set file to variable
// convert name of variable into lowercase and then give its value to newvar
set varnew=`echo var | sed 'y/ABCDEFGHIJKLMNOPQRSTUVW
if ($var==$varnew) then // compare the name when converted to lower case and the name at the beggining
echo '$file: file name aready in lower case!' // if same prints warning
// quite from the program
endif // end if
if (-e $varnew) then // check if any file with same name in lowercase exists
echo '$file = with lower case in exist, cant convert to lower case' // print warning
//quite
endif
mv $file $varnew // move value of varnew to file (change its name??)
endif
end
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
That doesn't matter as the script has #!/bin/sh as the first line. Simply copy the script in a file, give it execute permission and run it. It should display the warnings and mv command (doesn't execute it). If you are satisfied with the test, change the line
echo nv $file $filelower
to
mv $file $filelower
Yes there are several errors. Here is the updated script for c shell
#!/bin/csh
foreach file (*.cpp *.sh)
if (-d $file) then
echo "Warning: $file is Directory"
else
if (-f $file) then
set filelower=`echo $file | tr "[:upper:]" "[:lower:]"`
if ($file == $filelower) then
echo "$file : file name aready in lower case!"
else
if (-f $filelower) then
echo "$file = with lower case in exist, cant convert to lower case"
else
echo mv $file $filelower
endif
endif
endif
endif
end
You can make your csh script looks like:
#!/bin/csh
# use grep -v to get rid of all the dirs
foreach file ( `ls -al *.h *.cpp | grep -v ^d | awk '{print $9}' `)
set newfile = `echo $file | tr "[:upper:]" "[:lower:]"`
mv $file $newfile
end
exit
BTW, why not use ksh for script ? For shell scripting you should use sh/ksh/bash, it is a lot easy to do things than csh/tcsh.
please have a look at "Csh Programming Considered Harmful":
http://sc.tamu.edu/help/ot
the thing is it is not listing them at all... it just goes to the last step and if there is a file in upper case, it make it lower case, and print the mv $file $fileLower thats the only thing it prints
no warning about the files that are in lower case, neither the one which have one in lower case, and its not listing them , I cant figure out wats wrong with it :(
Run this and post the output...
#!/bin/csh
foreach file (*.cpp *.sh)
echo "Proceesing $file ..."
if (-d $file) then
echo "Warning: $file is Directory"
else
if (-f $file) then
set filelower=`echo $file | tr "[:upper:]" "[:lower:]"`
echo "Lowercase name $filelower"
if ($file == $filelower) then
echo "$file : file name aready in lower case!"
else
if (-f $filelower) then
echo "$file = with lower case in exist, cant convert to lower case"
else
echo mv $file $filelower
endif
endif
endif
endif
end
what do you mean "is it is not listing them at all"? my previous script will convert all the *.h *.cpp filenames to lower case, if it is alread lower case, if
will make no convertion.
you can modify it to:
#!/bin/csh
# use grep -v to get rid of all the dirs
foreach file ( `ls -al *.h *.cpp | grep -v ^d | awk '{print $9}' `)
set newfile = `echo $file | tr "[:upper:]" "[:lower:]"`
if ( -e $newfile ) then
echo " $newfile is already lower case name"
else
mv $file $newfile
endif
end
exit
PS:
ls -al *.h *.cpp | grep -v ^d | awk '{print $9}'
give you a list of all the files (excluding dirs), you might need to add
cd /mydir
in the beginning of the script
Business Accounts
Answer for Membership
by: amit_gPosted on 2006-01-15 at 00:11:18ID: 15703811
Bourne shell example...
#!/bin/sh
for file in *.cpp *.sh
do
[ -d $file ] && echo "Warning: $file is Directory" && continue
if [ -f $file ]
then
filelower=`echo $file | tr "[:upper:]" "[:lower:]"`
[ $file = $filelower ] && echo "$file: file name aready in lower case!" && continue
[ -f $filelower ] && echo "$file = with lower case in exist, cant convert to lower case" && continue
echo nv $file $filelower
fi
done