1833569 Members
4154 Online
110061 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
Deoncia Grayson_1
Honored Contributor

Re: scripting question

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 Catalyst")
echo "Enter the password for Deal"
su - deal;;

"Reset RF for Main Instance on Catalyst")
echo "Enter the password for Main"
su - main;;

"Exit")
exit;;
*)
echo "invalid option";;
esac

done
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sridhar Bhaskarla
Honored Contributor
Solution

Re: scripting question

OK. Here are the problems.

1. Modify the entire "select" phrase to one line. Or add "\" for better readability.
2. Change "Reset RF for Main Instance in Catalyst" in the select line to "Reset RF for Main Instance on Catalyst". As I said, we need to make sure these lines match exactly in select and in case statements. After modifying them, I got it like

//start
select clean_menu in \
"Verify Catalyst Packages" \
"Set Maintenance Flag on Catalyst" \
"Clearing Maintenance Flag on Catalyst" \
"Reset RF for Deal Instance on Catalyst" \
"Reset RF for Main Instance on 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 Catalyst")
echo "Enter the password for Deal"
su - deal;;

"Reset RF for Main Instance on Catalyst")
echo "Enter the password for Main"
su - main;;

"Exit")
exit;;
*)
echo "invalid option";;
esac

done



//end

-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 your help!! Now I'm just being annoying.. how do you get rid of the pound sign that shows up in front of the question mark, when i excecute my script, it brings up the menu but instead of just a question mark asking for my options I get...
#?

please help...
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sridhar Bhaskarla
Honored Contributor

Re: scripting question

You are supposed to replace the original part in your script. So, add these three lines just above the script.

clear
print "Catalyst Script MENU"
PS3="Test Menu, enter choice:"

It's PS3 that gives you the prompt. By default it is #?

-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

never mind..i fixed it, i think i'm coming down with dinner fixation.. no lunch... *sigh anyways, thanks for your help!! I really appreciate this!!

but I will have one more question about scripting... after switching user and successfully logging in, there is a process that needs to be ran that will ask for rfid and user id and I need these two things to be prompted before the process executes... should this be written in that actual script or can that be done seperately... any suggestions?
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sridhar Bhaskarla
Honored Contributor

Re: scripting question

Do you have to provide the arguments for the process or it itself will prompt?. In the first case, you will have write couple of lines to get that information in advance. For ex.,

"Reset RF for Deal Instance on Catalyst")
printf "Enter the rfid:"
read rfid
printf "Enter the user:"
read user
echo "Enter the password for Deal"
su - deal -c "
your_process $rfid $user
"

In the second case, all you have to do is to specify the process as an argument to "-c" and it will prompt itself.

I may go offline for sometime. But, continue to post and someone may answer your questions.

-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

thanks to everyone who contributed to this post. Sri, big thanks to you!

If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon