How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Ingo U Your Own Question

Ingo U
Ingo U, Software Engineer
Category: Homework
Satisfied Customers: 755
Experience:  Over 25 years experience in software development. Expert in Microsoft .Net, C#, C++, VB, SQL
44076068
Type Your Homework Question Here...
Ingo U is online now
A new question is answered every 9 seconds

The student is to modify the script student-example-p3

Customer Question

The student is to modify the script student-example-p3 following the instructions in the script.
The purpose of this script is to change the name of files/directories with spaces in the name to have a dash(-). So if a file/directory were named "A B", the new name would be A-B. The script will work on a directory specified on the command line and replace the spaces in the names for all files/directories underneath that directory.
The options to the script are as follows:
-f the -f says to only rename files
-d the -d says to only rename directories
Both -d and and -f may be specifed.
the name of the directory that you will be processing.
For example:
Given the following
$ ls
C D/ dirs.tar fix-spaces*
And the directory "C D" contained the following structure
./C D
./C D/E F
./C D/E F/G H
./C D/E F/G H/J K
./C D/E F/G H/J K/a b
./C D/E F/G H/a b
./C D/E F/a b
./C D/a b
After the script was run,
fix-space -f -d "C D"
the directory structure would be as follows:
C-D
C-D/E-F
C-D/E-F/G-H
C-D/E-F/G-H/a-b
C-D/E-F/G-H/J----K
C-D/E-F/G-H/J----K/a-b
C-D/E-F/a-b
C-D/a-b
Here is a copy of the script - it is alos as an attachment in both UNIX and Windows format.#!/usr/bin/kshUSAGE="$0 -f directory
$0 -d directory
$0 -d -f directory-f rename files
-d rename directories
"usage ()
{
print -u2 "$USAGE"
exit 1
}pathname ()
{
# function provided for the student
print -- "${1%/*}"
}basename ()
{
# function provided for the student
print -- "${1##*/}"
}find_dirs ()
{
# function provided for the student
find "$1" -depth -type d -name '* *' -print
}find_files ()
{
# function provided for the student
find "$1" -depth -type f -name '* *' -print
}my_rename()
{
# the student must implement this function to my_rename
# $1 to $2
# The following error checking must happen:
# 1. check if the directory where $1 resided is writeable,
# if not then report an error
# 2. check if "$2" exists -if it does report and error and don't
# do the mv command
# 3. check the status of the mv command and report any errors
: # remove this line after the function is coded
}fix_dirs ()
{
# The student must implement this function
# to actually call the my_rename funtion to
# change the name of the directory from having spaces to
# changing all of the spaces to -'s
# if the name were "a b", the new name would be a-b
# if the name were "a b" the new name would be a----b
: # remove this line after the function is coded
}fix_files ()
{
# The student must implement this function
# to actually call the my_rename funtion to
# change the name of the file from having spaces to
# changing all of the spaces to -'s
# if the name were "a b", the new name would be a-b
# if the name were "a b" the new name would be a----b
: # remove this line after the function is coded
}WFILE=
WDIR=
DIR=if [ "$#" -eq 0 ]
then
usage
fiwhile [ $# -gt 0 ]
do
case $1 in
-d)
WDIR=1
;;
-f)
WFILE=1
;;
-*)
usage
;;
*)
if [ -d "$1" ]
then
DIR="$1"
else
print -u2 "$1 does not exist ..."
exit 1
fi
;;
esac
shift
done# The student must implement the following:
# - if the directory was not specified, the script should
# print a message and exit# - if the Directory specified is the current directory, the script
# print a error message and exit# - if the directory specified is . or .. the script should print
# an error message and exit# - if both -f and -d are not specified, the script should print a
# message and exit
#if [ "$WDIR" -a "$WFILE" ]
then
fix_files "$DIR"
fix_dirs "$DIR"
elif [ "$WDIR" ]
then
fix_dirs "$DIR"
elif [ "$WFILE" ]
then
fix_files "$DIR"
fi
Submitted: 1 month ago.
Category: Homework
Expert:  Ingo U replied 1 month ago.

Hi,

please have a look at the following:

http://dl.dropbox.com/u/7895834/fix-spaces?dl=0

I believe it meets the requirements of the assignment. Let me know if you run into any issues.

Regards,

Ingo U

Customer: replied 1 month ago.
Could you please provide brief explanation to your coding?
Expert:  Ingo U replied 1 month ago.

OK, will do.

Expert:  Ingo U replied 1 month ago.

Here's a commented version:

https://dl.dropboxusercontent.com/u/7895834/fix-spaces-with-comments

Customer: replied 1 month ago.
Thanks for your answer.
I received an error as showing below when I run it:
Syntax error at line 176: `||' unexpected
Please advise
Expert:  Ingo U replied 1 month ago.

Hi,

how are you invoking the script? What OS and command shell are you running under?
I just tried the script in a few simple cases in a cygwin environment and it performed as expected...

Perhaps you could take (and upload) a screen shot of the window where you're trying it out to give me some context.

Customer: replied 1 month ago.
OS: Sun OS .....5.9 Generic c_12230-61 sun4u sparc SunW Sun-Fire-V240
command shell /bin/bash
I only created Folder C D
and run the command ( filename -f -d "C D")
Expert:  Ingo U replied 1 month ago.

OK, I don't immediately see a problem there...

but I haven't been around Sun OS in quite some time, and it's seems there are some differences in the ksh implementation..

Can you verify that the ksh shell is actually present (i.e. is there are /usr/bin/ksh ?)

Also, you can try this version (same link, new file)

http://dl.dropbox.com/u/7895834/fix-spaces-with-comments?dl=0

Not the change is only on the first line, to force using the traditional version of ksh as suggested here:

https://blogs.oracle.com/OTNGarage/entry/new_shell_in_oracle_solaris

I'm not sure it applies here because you seem to be on Solaris 9, not 11 (SunOS5.9 == Solaris 9) .. but worth a try anyway.

A final note; it is possible that somehow the script itself got corrupted during transfer to you... how did you get it to your machine?
Did you by any chance open it in a browser, select the text and past it into a local file (editor window)? THat can cause problems...

Expert:  Ingo U replied 1 month ago.

Hi, did you have any luck with my suggestions?

Customer: replied 1 month ago.
Thank you so much for asking I still have the same problem I use shellcheck.com to see what s wrong in the coding I found that something wrong on:
if [ "$WDIR" -a "$WFILE ]
then
fix_files "$DIR"
fix_dirs "$DIR"
elif [ "$WDIR" ]
then
fix_dirs "$DIR"
elif [ "$WFILE" ]
then
fix_files "$DIR"
fi
I am trying now to find what s wrong
Customer: replied 1 month ago.
Please look at the issues I have with the code and advise
Expert:  Ingo U replied 1 month ago.

Well I'm afraid the issues continue to look like they're due to the differences between Solaris and Linux/GNU, where I originally developed that script. The latest problem stems from the Solaris "find" utility not supporting the "-printf" switch - I found some comments about this here:

http://stackoverflow.com/questions/20377804/find-bad-option-printf-find-path-list-predicate-list-shell-scripting

As it suggests, if the GNU utilities (e.g. find) are installed on your Solaris box (under /opt/sfw perhaps, or /usr/sfw or /usr/local/bin) then we could adjust the script to use them explicitly rather than the Solaris 'find' .

It's hard to tell what other problems may exist, because once the first error in 'find' is encountered, the rest of the errors are most likely just a direct consequence and may just go away if we can use the GNU version of 'find'

I don't have access to a Solaris environment, so I'm afraid I can't rewrite the script using the constraints of the native Solaris tools, which could be even more tedious because your Solaris version is fairly old.

Here's another post related to that topic:

http://unix.stackexchange.com/questions/66415/solaris-default-install-user-tools

Customer: replied 1 month ago.
I still have a problem with this program I made some modification but still does not give the right result please help on this if you could find out what s wrong.please find the attached
Expert:  Ingo U replied 1 month ago.
THIS ANSWER IS LOCKED!

You need to spend $3 to view this post. Add Funds to your account and buy credits.