Turn Linux sudo command

Script using $ HOME variable Problem Description: Some of my colleagues had written a script that contains the following content.
The HOME} = $ {Bin_dir / Tools
TAIR_BIN_DIR the HOME = $ {} / tair_bin
TAIR_SRC_DIR the HOME = $ {} / tair_src

Old habits are:
direct admin login shell to run these scripts.

Personal account instead use sudo -u admin example.sh
after running discovery scripts in $ HOME variable to obtain an individual account's home directory.

Analysis: sudo when executing the command, the default home directory using a personal account assigned to the variable $ HOME.

For example, Joe Smith perform _sudo $ HOME result -u admin example.sh_ get is / home / zhangsan

The reason is that sudo command does not modify the default $ HOME variable, if you want to change the sudo in $ HOME variable,
you need to increase -H parameters: _sudo -u admin -H example.sh

For example as follows: [yuanqiao @ tair004013 ~] $ cat example.sh

#! / bin / sh BIN_DIR = $ {HOME} / tools
echo 'BIN_DIR =' $ BIN_DIR

[yuanqiao tair004013 @ ~] $ the sudo -u ADMIN ./example.sh
Bin_dir = / Home / yuanqiao / Tools

[yuanqiao@tair004013 ~]$ sudo -u admin -H ./example.sh
BIN_DIR=/home/admin/tools

Solution: Although sudo -H this parameter can solve the problem of the script, but does not recommend the use of sudo -H parameters,
because when the execution of the script do not always know the script inside the variable is how to define or acquired. Hope or write from the perspective of the script, the script to make appropriate changes to reduce dependence on environmental variables such account admin / root of.

Guess you like

Origin www.cnblogs.com/feiyun8616/p/11812582.html