GVKun编程网logo

将参数传递给bash中if语句中的函数(参数char*如何传参)

1

对于将参数传递给bash中if语句中的函数感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍参数char*如何传参,并为您提供关于Bash–如何将参数传递给通过标准输入读取的脚本、Bash–如何将

对于将参数传递给bash中if语句中的函数感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍参数char*如何传参,并为您提供关于Bash – 如何将参数传递给通过标准输入读取的脚本、Bash – 如何将参数传递给通过重定向标准输入读取的脚本、bash – 如何通过docker运行将参数传递给Shell脚本、bash – 将参数传递给命令时转义字符的有用信息。

本文目录一览:

将参数传递给bash中if语句中的函数(参数char*如何传参)

将参数传递给bash中if语句中的函数(参数char*如何传参)

我试图将参数传递给if语句中的函数,然后评估函数返回的内容(在bash中).我得到的结果是不正确的.你怎么做到这一点?
#!/bin/bash

foo() {
    if [ $1 = "zero" ]; then
        echo "0"
    else 
        echo "1"
fi
}

arg="zero"
if foo $arg -eq 0 ; then
    echo "entered 0"
else
    echo "entered something else"
fi

arg="blah"
if foo $arg -eq 0 ; then
    echo "entered 0"
else
    echo "entered something else"
fi

不是预期的结果:

cknight@macbook:~/work$./test.sh
0
entered 0
1
entered 0

先感谢您.
〜克里斯

您可以从函数返回结果,也可以使用$()来捕获其输出:
if [ $(foo $arg) -eq 0 ] ; then

Bash – 如何将参数传递给通过标准输入读取的脚本

Bash – 如何将参数传递给通过标准输入读取的脚本

我正在尝试从标准输入执行脚本,并传递参数。有办法吗?

让我们说我有以下几点:

cat script.sh | bash

我如何将参数传递给脚本?

我不想这样做:

bash script.sh arguments

也不是:

./script.sh arguments
在Linux上,
cat script.sh | bash /dev/stdin arguments

似乎工作

Bash – 如何将参数传递给通过重定向标准输入读取的脚本

Bash – 如何将参数传递给通过重定向标准输入读取的脚本

我想在“ Bash – How to pass arguments to a script that is read via standard input”帖子中再多一点.

我想创建一个脚本,它采用标准输入,并在传递参数的同时远程运行它.

我正在建立的脚本的简化内容:

ssh server_name bash <&0

如何采取以下接受参数的方法并将其应用于我的脚本?

cat script.sh | bash /dev/stdin arguments

也许我这样做不正确,请提供替代解决方案.

尝试这个:
cat script.sh | ssh some_server bash -s - <arguments>

bash – 如何通过docker运行将参数传递给Shell脚本

bash – 如何通过docker运行将参数传递给Shell脚本

我是码头世界的新人我必须调用一个 shell脚本,通过docker容器接收命令行参数.
例如:我的shell脚本看起来像:
#!bin/bash
echo $1

Dockerfile如下所示:

FROM ubuntu:14.04
copY ./file.sh /
CMD /bin/bash file.sh

我不知道如何在运行容器时传递参数

@ user1801934,你确定@Gilles Quenot的答案是正确的吗?

把arg1直接放到Dockerfile中,是你的请求吗?

这是我的答案

更新file.sh

#!/usr/bin/env bash
echo $1

使用存在的Dockerfile

构建形象:

docker build -t test .

用参数abc或xyz或其他东西运行图像.

docker run -ti test /file.sh abc

docker run -ti test /file.sh xyz

bash – 将参数传递给命令时转义字符

bash – 将参数传递给命令时转义字符

我经常使用我的Android手机将音频文件录制到手机的内部存储器中.我想编写一个Bash脚本来从手机中提取最新录制的文件并将其移动到我的桌面.这是我到目前为止所得到的:

adb shell find /storage/sdcard0/Sound\ Recordings/ | tail -1 | while read file ; do
    adb pull "$file" ~/Desktop/
done

但是,这失败了.似乎ADB没有正确地逃避事情,我在尝试运行脚本时收到以下消息:

' does not existstorage/sdcard0/Sound Recordings/20120817T065953.wav

它似乎忽略了双引号,当它试图运行时,一切基本上都会中断,因为它可能看到3个参数而不是两个,类似这样:

adb pull /storage/sdcard0/Sound Recordings/20120817T065953.wav ~/Desktop/

如何调整我的脚本以在必要时将反斜杠插入到我的$file变量中?在这种情况下,这是正确的解决方案吗?

解决方法:

由于您在另一个shell级别运行它,因此它会在子shell中运行之前进行扩展.解决此问题的最简单方法是转义文件名中的任何特殊字符:

adb shell find /storage/sdcard0/Sound\ Recordings/ | tail -1 | while IFS= read -r file ; do
    adb pull "$(printf %q "$file")" ~/Desktop/
done

今天的关于将参数传递给bash中if语句中的函数参数char*如何传参的分享已经结束,谢谢您的关注,如果想了解更多关于Bash – 如何将参数传递给通过标准输入读取的脚本、Bash – 如何将参数传递给通过重定向标准输入读取的脚本、bash – 如何通过docker运行将参数传递给Shell脚本、bash – 将参数传递给命令时转义字符的相关知识,请在本站进行查询。

本文标签: