1754016 Members
7549 Online
108811 Solutions
New Discussion юеВ

scripting question

 
SOLVED
Go to solution
Deoncia Grayson_1
Honored Contributor

scripting question

Proglem: I'm creating a menu and within the menu one of the options should switch user and ask for a password before executing the next command how would I do this?
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
16 REPLIES 16
Sridhar Bhaskarla
Honored Contributor

Re: scripting question

Hi Deonicia,

I assume you are using case statement. I would do like this

case $something in

"opt1")
some_commands
;;
"opt2")
echo "Enter the password for user"
su - user1 -c "
command1
command2
command3"
;;
*) echo "invalid choice"
;;
esac

If the option is "opt2", then it will switch user to user1 and then execute the commands enclosed in "". SU itself will ask for the password so you don't have write steps for it.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Mark Greene_1
Honored Contributor

Re: scripting question

something like this:

VALID=0
while [ $VALID -ne 1 ]; do

echo "Enter user name to become:\c"
read USERNAME
VALID=`grep -c "^$USERNAME" /etc/passwd`

done

su - $USERNAME


which will switch to the user requested, prompting for the user's password--unless being performed as root or that user.

mark
the future will be a lot like now, only later
john korterman
Honored Contributor

Re: scripting question

Hi,
an example of a menu where you have to supply the oracle user's pw for option no. 2, unless you are root:

PS3="Enter item: "
select choice in ls date quit
do
case $REPLY
in
1) ls;;
2) su oracle -c date;;
3|O|q) exit ;;
*) echo "Invalid choice" ;;
esac
done


regards,
John K.
it would be nice if you always got a second chance
Deoncia Grayson_1
Honored Contributor

Re: scripting question

clear
print "Catalyst Script MENU"
PS3="Test Menu, enter choice:"
select clean_menu in "Verify Catalyst Packages" "Set Maintenance Flag on Catalys
t" "Clearing Maintenance Flag on Catalyst" "Reset RF for Deal Instance on Cataly
st" "Reset RF for Main Instance in Catalyst" "Exit"
do
case $clean_menu in
"Verify Catalyst Packages")
/usr/sbin/cmviewcl;;

"Set Maintenance Flag on Catalyst")
/apps/bin/set_cat_maint_mode;;

"Clearing Maintenance Flag on Catalyst")
/apps/bin/clr_cat_maint_mode;;

"Reset RF for Deal Instance on Catalsyst")
echo "Enter the password for user"
su - root;;

an example of the script i'm using and when i choose option 4 it doesn't do anything.
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sridhar Bhaskarla
Honored Contributor

Re: scripting question

Hi Deoncia,

Couple of problems in your script.

1. Correct the string "catalsyst" in option4.
2. Add option5 and option 6 also like

"Reset RF for Deal Instance on Catalyst")
echo "Enter the password for user"
su - root;;
"Reset RF for Main Instance in Catalyst"
some_function;;
"Exit")
exit;;
esac

done

The text has to match exactly... otherwise it won't work.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Deoncia Grayson_1
Honored Contributor

Re: scripting question

Hey Sri,

Thanks for heads up on the typo; however now I'm getting a syntax error on my Exit line. Its saying `) is unexpected.
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sridhar Bhaskarla
Honored Contributor

Re: scripting question

Looks like you copy-pasted my lines.. That was only to show you the modifications - not to copy them exactly.. Let me try then..

"Reset RF for Deal Instance on Catalyst")
echo "Enter the password for user"
su - root;;
"Reset RF for Main Instance in Catalyst")
some_function;;
"Exit")
exit;;
*)
echo "invalid option";;
esac

done

You will need to fill in "some_function".

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Deoncia Grayson_1
Honored Contributor

Re: scripting question

Hey Sri,

Thanks for the help thus far, really appreciate it; however, one more question, on option five I need to su to another user id and when I plugged that option in, I'm receiving invalid option.

"Reset RF for Deal Instance on Catalyst")
echo "Enter the password for Deal"
su - deal;;

"Reset RF for Main Instance on Catalyst")
echo "Enter the password for Main"
su - main;;
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sridhar Bhaskarla
Honored Contributor

Re: scripting question

Can you post the the script from 'select' until 'done' again?. Looks like there are some more mismatches.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try