简体   繁体   English

需要更改 shell 脚本里面的目录

[英]Need to change the directory inside the shell script

I need to change the directory inside the shellscript.i have tried below我需要更改 shellscript 中的目录。我在下面尝试过

#!/bin/bash
sudo -u rv bash
cd /opt/test
source /opt/rv/van.env
./rv.sh |grep "STATUS" 

When I tried it didn't cd to that path and not executed th rv.sh.guide me with some ideas.当我尝试时,它没有cd到那条路径,也没有执行 rv.sh.guide 我有一些想法。

Fyi rv.sh path /opt/test/仅供参考 rv.sh 路径 /opt/test/

The following line:以下行:

sudo -u rv bash

Will launch a bash console as the user rv and then your entire script will halt until the program (ie: bash) ends.将以用户rv的身份启动 bash 控制台,然后您的整个脚本将停止,直到程序(即:bash)结束。

Only after bash ends, the program will continue execution and run the last two lines.只有bash结束后,程序才会继续执行,运行最后两行。

The last two lines are running as part of the first script, therefor, they run as your user.最后两行作为第一个脚本的一部分运行,因此,它们以您的用户身份运行。

It seems like you're expecting sudo to execute the rest of the script in the users context but it is not interactive.您似乎希望sudo在用户上下文中执行脚本的 rest,但它不是交互式的。 It will invoke a separate bash instance and, once exited, resume the rest of the script afterwards.它将调用一个单独的 bash 实例,并在退出后恢复脚本的 rest。

You'll have to put together a separate script and run that with sudo.您必须编写一个单独的脚本并使用 sudo 运行它。 Running sudo inside of scripts is rarely a good idea.在脚本中运行 sudo 很少是一个好主意。 Sa https://askubuntu.com/questions/425754/how-do-i-run-a-sudo-command-inside-a-script Sa https://askubuntu.com/questions/425754/how-do-i-run-a-sudo-command-inside-a-script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM