将空参数传递给sudo -i [英] Passing empty arguments to sudo -i

查看:61
本文介绍了将空参数传递给sudo -i的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想使用 sudo -i 运行的bash脚本示例:

Here is an example bash script that I would like to run using sudo -i:

#!/bin/bash

echo "arg 1: $1"
echo "arg 2: $2"

当我使用一个空参数正常运行此命令时,它将按预期运行:

When I run this command normally with one empty argument, it runs as expected:

$ /tmp/args.sh "" two
arg 1:
arg 2: two

使用简单的sudo,我得到了预期的结果:

With plain sudo, I get the expected result:

$ sudo /tmp/args.sh "" two
arg 1:
arg 2: two

但是,如果我使用 -i (拾取用户的外壳程序和登录脚本),第一个参数突然消失:

However if I use -i (to pick up the user's shell and login scripts), suddenly the first argument disappears:

$ sudo -i /tmp/args.sh "" two
arg 1: two
arg 2:

我无法找出任何方式引用或转义第一个空参数.

And I cannot figure out any way to quote or escape the first, empty argument.

注意:我想出了一种解决方法,将/tmp/args.sh"两个写入文件,然后使用 sudo -i执行,但是我想知道是否有任何方法可以直接从命令行实现.

Note: I have figured out a workaround by writing /tmp/args.sh "" two to a file and then executing that with sudo -i, but I was wondering if there is any way to achieve this directly from the command line.

推荐答案

这对我有用:

sudo -i bash -c '/tmp/args.sh "" two'

这篇关于将空参数传递给sudo -i的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆