ksh 脚本 +在 shell 脚本中打印参数内容

发布于 01-03 10:30 字数 569 浏览 4 评论 0原文

我想用一个参数运行 script.sh

如果第一个参数 = 操作,则 script.sh 将打印操作参数 - 每 1 分钟重新启动一次机器

我的示例不起作用,但请建议脚本中需要修复哪些内容,以便如果参数是 action,我将打印 $action 参数。

备注我不想设置以下解决方案 - [[ $1 = action ]] && echo 操作“每 1 分钟重新启动计算机

我的示例脚本:

  #!/bin/ksh
  action="restart machine each 1 min"
  echo  "action" ${$1}

如何运行脚本的示例

  ./script.sh action

我需要获得的预期结果:

    action restart machine each 1 min

I want to run the script.sh with one argument.

If the first argument = action then script.sh will print the action parameter - restart machine each 1 min

My example not work but please advice what need to fix in the script so I will print the $action parameter if argument is action.

Remark I not want to set the following solution - [[ $1 = action ]] && echo action "restart machine each 1 min

My example script:

  #!/bin/ksh
  action="restart machine each 1 min"
  echo  "action" ${$1}

Example how to run the script

  ./script.sh action

Expected results that I need to get :

    action restart machine each 1 min

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈场末日恋爱2025-01-10 10:30:44

好的,使用 pdksh 可以实现:

echo  "action" `eval echo '
$1`

Well with pdksh this works:

echo  "action" `eval echo '
$1`
浅浅淡淡2025-01-10 10:30:44

您想使用 eval:

action="restart machine each 1 min"
eval echo $1 \$1

请注意,这样做会带来巨大的安全风险。考虑一下如果用户使用第一个参数“; rm -rf /”调用脚本会发生什么

您可能可以通过以下方式缓解此类问题:

eval "echo '$1' \"\$1\""

但实际上您只是自找麻烦(如果第一个参数包含双精度数,则最后一个版本将陷入困境-quote 和 $() 构造将允许执行任意命令)。简单地使用 case 语句并检查参数是否与您正在查找的字符串完全匹配会更安全。或者,至少检查您正在评估的参数是否不包含以下任何字符:;()$"'。检查它是否仅包含字母数字 (a-zA-Z0-9)

You want to use eval:

action="restart machine each 1 min"
eval echo $1 \$1

Note that doing something like this is a huge security risk. Consider what happens if the user invokes the script with the first argument "; rm -rf /"

You can probably alleviate such problems with:

eval "echo '$1' \"\$1\""

but really you're just asking for trouble (This last version will struggle if the first argument contains a double-quote, and a $() construct will permit an arbitrary command to be executed). It is much safer to simply use a case statement and check that the argument matches exactly a string that you are looking for. Or, at least check that the argument you are eval'ing does not contain any of the following characters: ;()$"'. It's probably safest to check that it only contains alphanumerics (a-zA-Z0-9)

舟遥客2025-01-10 10:30:44

已经过去两年了,但这里有一个使用 nameref (又名 typeset -N)的示例。
它包括对给定论点有效性的三个连续测试。

  1. 是否给出论证?
  2. 参数与已知变量匹配吗? nameref 检查这一点。
  3. 目标变量是否已设置值?
action='This is the value of $action'
word='This it the value of ${word}'
list='This is a list'
lie='This is a lie'

(
typeset name=${1:?Usage: script.sh varname} || exit 
nameref arg1=${name} || exit

: ${arg1:?} || exit

echo "$name $arg1"
)

It's been two years, but here's an example of using nameref (a.k.a. typeset -N).
It includes three consecutive tests for validity of the given argument.

  1. Is an argument given?
  2. Does the argument match a known variable? nameref checks this.
  3. Does the target variable have a value set?
action='This is the value of $action'
word='This it the value of ${word}'
list='This is a list'
lie='This is a lie'

(
typeset name=${1:?Usage: script.sh varname} || exit 
nameref arg1=${name} || exit

: ${arg1:?} || exit

echo "$name $arg1"
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文