GVKun编程网logo

Bash:if [“echo test”==“test”];然后回声“回声测试输出测试shell”fi;可能?

25

对于想了解Bash:if[“echotest”==“test”];然后回声“回声测试输出测试shell”fi;可能?的读者,本文将是一篇不可错过的文章,并且为您提供关于BashShell:CheckF

对于想了解Bash:if [“echo test”==“test”];然后回声“回声测试输出测试shell”fi;可能?的读者,本文将是一篇不可错过的文章,并且为您提供关于Bash Shell: Check File Exists or Not、bash – “export”在shell编程中做什么?、bash – 从shell脚本运行jest测试 – 有失败导致脚本退出、bash – 如何在交互式shell脚本中使“echo”ed行可编辑?的有价值信息。

本文目录一览:

Bash:if [“echo test”==“test”];然后回声“回声测试输出测试shell”fi;可能?

Bash:if [“echo test”==“test”];然后回声“回声测试输出测试shell”fi;可能?

是否可以使用bash从 shell执行命令,如果它返回某个值(或空值)执行命令?

if [ "echo test" == "test"]; then
  echo "echo test outputs test on shell"
fi

解决方法

是的,您可以使用反引号或$()语法:

if [ $(echo test) = "test" ] ; then
  echo "Got it"
fi

你应该用$替换$(echo test)

"`echo test`"

要么

"$(echo test)"

如果您运行的命令的输出可以为空.

并且POSIX“stings相等”测试运算符=.

Bash Shell: Check File Exists or Not

Bash Shell: Check File Exists or Not

How do I test existence of a text file in bash running under Unix like operating systems?

You need to use the test command to check file types and compare values. The same command can be used to see if a file exist of not. The syntax is as follows:

test -e filename
[ -e filename ]
 
test -f filename
[ -f filename ]

The following command will tell if a text file called /etc/hosts exists or not using bash conditional execution :

[ -f /etc/hosts ] && echo "Found" || echo "Not found"

Sample outputs:

Found

The same code can be converted to use with if..else..fi which allows to make choice based on the success or failure of a test command:

#!/bin/bash
file="/etc/hosts"
if [ -f "$file" ]
then
	echo "$file found."
else
	echo "$file not found."
fi

File test operators

The following operators returns true if file exists:

-b FILE
              FILE exists and is block special

       -c FILE
              FILE exists and is character special

       -d FILE
              FILE exists and is a directory

       -e FILE
              FILE exists

       -f FILE
              FILE exists and is a regular file

       -g FILE
              FILE exists and is set-group-ID

       -G FILE
              FILE exists and is owned by the effective group ID

       -h FILE
              FILE exists and is a symbolic link (same as -L)

       -k FILE
              FILE exists and has its sticky bit set

       -L FILE
              FILE exists and is a symbolic link (same as -h)

       -O FILE
              FILE exists and is owned by the effective user ID

       -p FILE
              FILE exists and is a named pipe

       -r FILE
              FILE exists and read permission is granted

       -s FILE
              FILE exists and has a size greater than zero

       -S FILE
              FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE
              FILE exists and its set-user-ID bit is set

       -w FILE
              FILE exists and write permission is granted

       -x FILE
              FILE exists and execute (or search) permission is granted

 

(Fig.01: File test operators taken from bash man page)


The syntax is same (see File operators (attributes) comparisons for more info):

 

if [ operator FileName ]
then
     echo "FileName - Found, take some action here"
else
   echo "FileName - Not found, take some action here"
fi

bash – “export”在shell编程中做什么?

bash – “export”在shell编程中做什么?

根据我可以告诉,变量赋值是相同的,无论是或没有前面的“导出”。这是为了什么?
导出的变量(例如$ HOME和$ PATH)可用于其他程序。
其他程序不能使用常规(非导出)变量。
$ env | grep '^variable='
$                                 # No environment variable called variable
$ variable=Hello                  # Create local (non-exported) variable with value
$ env | grep '^variable='
$                                 # Still no environment variable called variable
$ export variable                 # Mark variable for export to child processes
$ env | grep '^variable='
variable=Hello
$
$ export other_variable=Goodbye   # create and initialize exported variable
$ env | grep '^other_variable='
other_variable=Goodbye
$

有关更多信息,请参阅GNU Bash手册中的export builtin条目。

请注意,非导出的变量将可用于通过(…)运行的子shell和类似的符号:

$ othervar=present
$ (echo $othervar; echo $variable; variable=elephant; echo $variable)
present
Goodbye
elephant
$ echo $variable
Goodbye
$

当然,subshel​​l不能影响父shell中的变量。

关于subshel​​l的一些信息可以在Bash手册中的command grouping和command execution environment下找到。

bash – 从shell脚本运行jest测试 – 有失败导致脚本退出

bash – 从shell脚本运行jest测试 – 有失败导致脚本退出

我有一个普通的package.json,使用npm test命令运行 jest个测试. package.json的相关部分如下所示:

{
  "scripts": {
     "test": "jest","jest": {
    "scriptPreprocessor": "../shared/preprocessor.js"
  }
}

shell脚本如下所示:

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
parent_dir="$script_dir/.."

echo
echo "Running all tests..."
find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;

现在,当一个jest测试失败时,bash脚本不会以失败状态退出.我希望它能够与Jenkins一起创建CI循环.开玩笑测试失败的示例如下:

FAIL  js/components/__tests__/ExperienceCampaign-tests.js (13.19s)
● ExperienceCampaign › listening to ExperienceCampaignStore › it starts listening when the component is mounted
  - Expected: true toBe: false
        at Spec.<anonymous> (/Users/jonathan/experience-studio/campaign/js/components/__tests__/ExperienceCampaign-tests.js:54:26)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
1 test Failed,0 tests passed (1 total)
Run time: 13.748s
npm ERR! Test Failed.  See above for more details.
npm ERR! not ok code 0

任何帮助表示赞赏.

解决方法

jest有一个–forceExit标志,可用于…强制退出:D.
只需将其添加到您的脚本中.

您可能在测试中做了一些异步操作,并没有及时返回回调.可能想仔细看看.

{
  "scripts": {
     "test": "jest --forceExit","jest": {
    "scriptPreprocessor": "../shared/preprocessor.js"
  }
}

bash – 如何在交互式shell脚本中使“echo”ed行可编辑?

bash – 如何在交互式shell脚本中使“echo”ed行可编辑?

我有以下问题:在交互式脚本中,当要求输入时,我想显示一个建议并使其可编辑.它与命令提示符中的“向上箭头和编辑最后一个命令”类似,除非没有“向上箭头”.到目前为止,我尝试了几种不同的东西但没有成功

这些是我尝试过的东西:

1)从编辑器获取输入,如下所示:

echo "$SUGGESTION\c"
INPUT=`ed -` # problem with this approach is that 'ed' starts in command mode 
             # by default,and I would need input mode

2)使用read -e

echo "$SUGGESTION\c"
read -e INPUT # doesn't work as advertised

经过广泛的谷歌搜索,我确信2)应该工作,但事实并非如此.首先,我不能先删除$SUGGESTION而不先输入一些输入;键入一些字符后,退格键会删除整行,而不只是一个字符.

所以我的问题是:如何使“read -e”工作或是否有另一种方法来解决这个问题?非常感激您的帮忙!

解决方法

它确实像宣传的那样工作,但你需要一个额外的参数来做你想要的:

read -e -i "$SUGGESTION" INPUT

不幸的是,这只适用于Bash 4.

如果你有一个C编译器和readline可用,这里有一个你可以使用的快速入侵.将以下内容保存到myread.c(或其他)并编译它(您需要与readline链接).对于GCC,那将是:gcc -o myread myread.c -lreadline.

#include <stdio.h>
#include <readline/readline.h>

int main(int argc,char **argv)
{
    if (argc != 2)
        return 1;

    // stuff the input buffer with the default value
    char *def = argv[1];
    while (*def) {
        rl_stuff_char(*def);
        def++;
    }

    // let the user edit
    char *input = readline(0);
    if (!input)
        return 1;
    // write out the result to standard error
    fprintf(stderr,"%s",input);
    return 0;
}

你可以像这样使用它:

myread "$SUGGESTION" 2> some_temp_file
if [ $? -eq 0 ] ; then 
  # some_temp_file contains the edited value
fi

有很大的改进空间,但我想这是一个开始.

我们今天的关于Bash:if [“echo test”==“test”];然后回声“回声测试输出测试shell”fi;可能?的分享就到这里,谢谢您的阅读,如果想了解更多关于Bash Shell: Check File Exists or Not、bash – “export”在shell编程中做什么?、bash – 从shell脚本运行jest测试 – 有失败导致脚本退出、bash – 如何在交互式shell脚本中使“echo”ed行可编辑?的相关信息,可以在本站进行搜索。

本文标签: