1834448 Members
1902 Online
110067 Solutions
New Discussion

Script Help

 
SOLVED
Go to solution
OldSchool
Honored Contributor

Script Help

I have the following script that is giving me some problems.
I need it to take users to different instances on an HP-UX server. This is what I have, that's not working though. Maybe someone can help me out here:

#!/bin/ksh
clear
cmd="cd"
echo "What Region ID would you like to login to Today?\n"
echo "\t1) EAST"
echo "\t2) WEST"
echo "\t3) exit"
echo "\nPlease enter choice here ->\c"
read target
echo " --$targ--- "
case $target in
1) target= "cd /east/"
echo " $target "
;;
2) target= "cd /west/"
echo " $target "
;;
3) target= "exit"
echo " $target "
;;
4) echo " Please enter 1,2, or 3 "
exit 0
;;
esac
#clear
echo ">\c"
echo "\nSession Completed, If you would like to exit the system\c"
echo ""
echo "\nType exit, to leave the System\c"
echo "\n\c"
#sleep 2
exit 0
23 REPLIES 23
Fred Ruffet
Honored Contributor

Re: Script Help

What is not working ? What do you want this script to do ?

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Peter Godron
Honored Contributor

Re: Script Help

Tom,
when you say the script is not working, what is the failure/expected behaviour?
All I should see was that the 1),2) and 3) lines should not have a space between = and "cd..."
Regards
Peter Godron
Honored Contributor

Re: Script Help

Tom,
if you want the script to loop until the user selects 3 to exit, you will need a while statement.
Regards
OldSchool
Honored Contributor

Re: Script Help

The script will not change directories and give the users the correct directory and .profile.
Florian Heigl (new acc)
Honored Contributor

Re: Script Help

case $1 in
3)
mv $origpfad/* $sichpfad/
;;

4)
mv $sichpfad/* $origpfad/
;;
esac

if I remember correctly, You must either
write
4) mv $sichpfad/* $origpfad/;
;
or
4)
mv $sichpfad/* $origpfad/
;;

(but I'm not really sure.

Best thing would be using
#/bin/ksh -vx
to enable debugging
yesterday I stood at the edge. Today I'm one step ahead.
Bill Hassell
Honored Contributor

Re: Script Help

Misspelled variable: --$targ-- should be --$target--

4) choice is incorrect. The case statement is actually a series of regular expression so if 4 is entered, your script would exit. What you probably want is anything not already seen above, so it should be *) not 4).

To avoid problems in the future, always start your script with the correct location for the interpreter: #!/usr/bin/ksh (there is no directory called /bin...the V.4 filesystem layout for user commands is /usr/bin, and /bin is only a symlink that will be removed someday)

To find spelling errors in scripts, start the script with set -u and that would have caught the --$targ-- error.


Bill Hassell, sysadmin
Gordon  Morrison_1
Regular Advisor

Re: Script Help

Hi Tom,
You don't say exactly what the problem is.
The first thing I would say is to remove the spaces after the equal signs
i.e.
1) target= "cd /east/"
should be
1) target="cd /east/"

I assume this is just a little test script and that you intended just to echo the commands rather than executing them.
If you want to stop users from "falling off the end" of the script, put the case inside an infinite loop:

while true
do
echo "What Region ID would you like to login to Today?\n"
...
...
case ...
blah
blah
esac
clear
done
What does this button do?
Steve Steel
Honored Contributor

Re: Script Help

Hi


You have had some good advice here


Look at

www.shelldorado.com

for more


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Peter Godron
Honored Contributor

Re: Script Help

Tom,
if the user selects 1 you want them to go to a directory /east?
1) target="cd /east"
$target
;;
Please note the lack of space marker between = and " !!!
Regards
V. Nyga
Honored Contributor

Re: Script Help

Hi,

yes, it would be fine, to know where the script is failing.
Which 'echo's' are working?

By the way:
read target
echo " --$targ--- "
I can't imagine that this will work, --$targ--- isn't defined.

HTH
Volkmar

*** Say 'Thanks' with Kudos ***
OldSchool
Honored Contributor

Re: Script Help

When the script exits, it switches back to the original directory of the users.
Peter Godron
Honored Contributor

Re: Script Help

Tom,
if you want to return to the original point:
orig=`pwd`
$target
.
.
.
cd $orig
Regards
OldSchool
Honored Contributor

Re: Script Help

The problem is when the script exits, it places the user back in their original directory. That would be good for some, but for others I need them to stay in the directory the script cd's them into.
Gordon  Morrison_1
Regular Advisor
Solution

Re: Script Help

To make the cd in the scriopt apply to the user's shell after exiting the script, you need to "source" it:
. /path/to/script
^
Note space!

Try the following:
#!/bin/ksh
while true
do
clear
cmd="cd"
echo "What Region ID would you like to login to Today?\n"
echo "\t1) EAST"
echo "\t2) WEST"
echo "\t3) exit"
echo "\nPlease enter choice here ->\c"
read target
echo " $target "
case $target in
1) target="cd east/"
echo " $target "
$target
;;
2) target="cd west/"
echo " $target "
$target
;;
3) target="exit"
echo " $target "
break
;;
*) echo " Please enter 1,2, or 3 "
;;
esac
done
pwd
echo ">\c"
echo "\nSession Completed, If you would like to exit the system\c"
echo ""
echo "\nType exit, to leave the System\c"
echo "\n\c"
What does this button do?
OldSchool
Honored Contributor

Re: Script Help

The script goes into an infinate loop, never exiting until the user selects 3. Then it just places the user back into thier own directory again.
Florian Heigl (new acc)
Honored Contributor

Re: Script Help

Tom, if possible, please run it in debug mode (-vx) and post the output until it's looped for the first time.
yesterday I stood at the edge. Today I'm one step ahead.
OldSchool
Honored Contributor

Re: Script Help

Here is all I got from running it with the -vx.

^[[2J^[[HWhat Region ID would you like to login to Today?

1) EAST
2) WEST
3) exit

Please enter choice here -> 2
cd /west/
^[[2J^[[HWhat Region ID would you like to login to Today?

1) EAST
2) WEST
3) exit

Please enter choice here ->2
bfedcat /west/
^[[2J^[[HWhat Region ID would you like to login to Today?
OldSchool
Honored Contributor

Re: Script Help

Script stays in an infinate loop, then when I exit it kicks me back to the original directory. This even after I source the file.
Gordon  Morrison_1
Regular Advisor

Re: Script Help

The following script will exit after making a valid selection. If you source it, it will leave you in the directory of your choice:
. ./script.ksh
(dot space dotslashscriptname)

#!/bin/ksh
while true
do
clear
cmd="cd"
echo "What Region ID would you like to login to Today?\n"
echo "\t1) EAST"
echo "\t2) WEST"
echo "\t3) exit"
echo "\nPlease enter choice here ->\c"
read target
echo " $target "
case $target in
1) target="cd /east/"
echo " $target "
$target
break
;;
2) target="cd /west/"
echo " $target "
$target
break
;;
3) target="exit"
echo " $target "
break
;;
*) echo " Please enter 1,2, or 3 "
sleep 2
;;
esac
done
pwd
echo ">\c"
echo "\nSession Completed, If you would like to exit the system\c"
echo ""
echo "\nType exit, to leave the System\c"
echo "\n\c"
What does this button do?
OldSchool
Honored Contributor

Re: Script Help

Where exactly would I place the source?
Gordon  Morrison_1
Regular Advisor

Re: Script Help

"Sourcing" is a term used in Unixland which basically means "Run this in the current shell, rather than a subshell"
You do this by typing a dot followed by a space BEFORE the name of the script or executable. e.g.
. script
If you don't type a dot and a space before the script name, it will run in a subshell, and that subshell will exit when the script finishes running, taking any changes made in the script with it.

. script
What does this button do?
OldSchool
Honored Contributor

Re: Script Help

Thank you all for all the help, the script is now working from the users .profile.

Thanks again,

Tom
OldSchool
Honored Contributor

Re: Script Help

With all the help, I was able to get it put together and working. Thanks all