LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sudo in bash script grep environment (https://www.linuxquestions.org/questions/programming-9/sudo-in-bash-script-grep-environment-915934/)

geep1980 11-28-2011 01:00 AM

sudo in bash script grep environment
 
I am creating a script to stop and start a application.

The script has to do a sudo and then start a script.
The path to the script begins with a variable which is set when logging on. This path isn't exported so I can't use the environment variable.
I am no trying to grep the variable but it isn't working.

I am doing the following
Code:

sudo su - user
DIRECTORY="$( env | grep -i appl
echo ${DIRECFTORY}


The echo stays empty.
Can someone help me with this problem.

corp769 11-28-2011 01:23 AM

Hello,

Check your spelling of "DIRECTORY" within your echo statement... That might be your problem. Also, you seem to be missing some code... Could you edit your post and make it reflect your original code? Thanks!

Cheers,

Josh

zedmelon 11-28-2011 01:38 AM

Assuming all is well beyond that, I'm dubious whether you can su to another user from within a shell script. You probably get the output of the echo statement until after you exit from the other user's shell. Even if you background the shell, you've still only run the script as your original user.

Could you split this into two scripts? The last two lines of the first could be
Code:

D="$( env | grep -i appl)"
sudo <script2> $D

and the second script would start with
Code:

D="$*"
Depending on what you're trying to do, you could also try
Code:

man setuid

geep1980 11-28-2011 01:52 AM

The complete script I have is

Code:

ssh SERVER <<HERE
sudo su - user
#!/bin/bash
DIRECTORY="$( env | grep -i appl )"

${DIRECTORY}/server/stop

echo "End stop script."

It goes to the server I want and it does a sudo
Only the directory variable can't be filled

corp769 11-28-2011 01:56 AM

First of all, "#!/bin/bash" should be at the very top of your script. Second of all, can you verify for me that the command you are trying to store in your variable works? Lastly, after you store data in a variable, the correct way to call it is the following:
Code:

DIRECTORY="$(env | grep -i appl)"
echo $DIRECTORY

Using $() executes whatever is within the parenthesis; So using "echo $(DIRECTORY)" is trying to execute whatever is stored within DIRECTORY, which would not display anything.

geep1980 11-28-2011 02:07 AM

When I sudo to the user and run the script to get the variable filled it works

When running the script as my own user and having it sudo and then fill the variable isn't working

corp769 11-28-2011 02:10 AM

So then have you tried using sudo with the command itself?
Code:

DIRECTORY="$(sudo env | grep -i appl)"

geep1980 11-28-2011 02:13 AM

When I try this I get the following reply

Quote:

Sorry, user steenho is not allowed to execute '/bin/env' as root

corp769 11-28-2011 02:16 AM

Try sudo with the -u flag and tell it to manually use the root user to execute env. That would be part of the problem, as it is trying to use your normal user. How is your sudo configuration setup, anyway?

geep1980 11-28-2011 02:23 AM

I still get the same message.
I am not able to view the /etc/sudoers file. But I know it isn't possible to do something like

Quote:

sudo su - USER ls -rtl

corp769 11-28-2011 03:04 AM

Why can't you view that file? You are indeed the administrator of the box you are on, correct?

zedmelon 11-28-2011 03:39 AM

Are you certain you can't predict the directory? Why not?

Are you connecting to a Windows box running Cygwin and grepping for "Application Data" or something?

Please post the output of
Code:

sudo -l
(that's a lower case "L")

geep1980 11-28-2011 04:05 AM

I am not the administrator off the machine, I am a user (Technical Application Manager)

The output off sudo -l is

Quote:

Matching Defaults entries for steenho on this host:
log_year, logfile=/var/log/sudolog, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR
LS_COLORS MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT
LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET
XAUTHORITY", !authenticate

Runas and Command-specific defaults for steenho:


User steenho may run the following commands on this host:
(root) NOPASSWD: /opt/opsware/agent_tools/get_all_cust_attr.sh, /opt/opsware/agent_tools/get_cust_attr.sh,
/opt/opsware/agent_tools/get_info.sh, /usr/sbin/pvs, /usr/sbin/vgs, /usr/sbin/lvs, /bin/tracert
(root) /bin/su - USER, /bin/su - oracle, /bin/su - USER, /bin/su - USER, /bin/su - USER



corp769 11-28-2011 04:19 AM

There's your problem. You need to get your administrator to give you access, plain and simple.

geep1980 11-28-2011 05:07 AM

I will contact them.
Thank you for the help.


All times are GMT -5. The time now is 09:05 AM.