AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题

问题[test](unix)

Martin Hope
Lex
Asked: 2023-06-18 01:03:01 +0800 CST

语法错误:使用计算时出现“(”意外(期望“fi”)

  • 5

我对 shell 比较陌生,并且遇到语法错误,但我仍然感到困惑。

#!/bin/dash

ls="ls -l test"
small=0
medium=0
large=0

for i in $(seq 11 9 56)
do
    filename=$(echo $($ls) | cut -d ' ' -f $i)
    count=$(wc -w test/$filename | cut -d ' ' -f 1)
    if [ "$count" -lt 10 ]; then
        small=(( $small+1 ))
    elif [ "$count" -gt 100 ]; then
        large=(( $large+1 ))
    else
        medium=(( $medium+1 ))
    fi
done
echo $small
echo $medium
echo $large

在这段代码中,我试图获取文件列表,检索文件有多少个单词,并将它们分类为小/中/大尺寸文件。我很确定我在使用 递增变量时没有犯任何语法错误test,但它们似乎在 if 语句中不起作用。谁能帮我解决这个问题吗?在 if 语句中使用多个括号时出现语法错误,但我不知道它是什么。

test
  • 2 个回答
  • 52 Views
Martin Hope
Prince
Asked: 2022-06-14 01:25:24 +0800 CST

if语句中的多个条件

  • 0

我正在做一个基本问题,即通过在第一和第二个用户输入中添加、产品、减法、除法我不明白我哪里出错了,因为它没有通过任何测试用例。

约束:- -100<=x,y<=100, y != 0

read -p "enter first number:" first
read -p "enter second number:" second
if [[ ("$first" -ge "-100"  -a "$first" -ge "100") -a ("$second" -ge "-100" -a "$second" -ge "100") ]]
then
    if [ $second -ne 0 ]
    then    
        echo "$first + $second" | bc
        echo "$first - $second" | bc
        echo "$first * $second" | bc
        echo "$first / $second" | bc
    fi
fi
'''
shell-script test
  • 1 个回答
  • 280 Views
Martin Hope
Dawson Smith
Asked: 2022-01-11 01:47:19 +0800 CST

语句的含义-test -x /usr/bin/find || 出口 0

  • 4

我只是想了解以下陈述的含义以及它们是否正确。

test -x /usr/bin/find || exit 0 
Command 1
Command 2
Command 3

的输出test -x /usr/bin/find始终为 0。这意味着将执行 exit 0 命令,这意味着永远不会执行命令 1、2、3。我在这里吗?

shell test
  • 2 个回答
  • 555 Views
Martin Hope
Martian2020
Asked: 2021-11-09 05:45:40 +0800 CST

-a(文件存在)的 Bash 否定不会改变结果,而对于 ! -e 改变结果

  • 16

我很困惑,但仍然猜想我以某种方式误解了 Bash。

/$ if [   -e /bin/grep ]; then echo yea; else echo nay ; fi
yea
/$ if [ ! -e /bin/grep ]; then echo yea; else echo nay ; fi
nay
/$ if [   -a /bin/grep ]; then echo yea; else echo nay ; fi
yea
/$ if [ ! -a /bin/grep ]; then echo yea; else echo nay ; fi
yea

为什么否定!会逆转-e测试而不是-a测试的效果?

男人 bash 说:

测试:

3 论据

以下条件按所列顺序应用。

  1. 如果第二个参数是上面在条件表达式下列出的二进制条件运算符之一,则表达式的结果是使用第一个和第三个参数作为操作数的二进制测试的结果。当存在三个参数时,-aand运算符被视为二元运算符。-o
  2. 如果第一个参数是!,则该值是使用第二个和第三个参数对双参数测试的否定。

Bash 条件表达式

条件表达式由[[ 复合命令test和[内置命令使用

-a file
如果文件存在则为真。
-b file
如果文件存在并且是块特殊文件,则为真。
-c file
如果文件存在并且是字符特殊文件,则为真。
-d file
如果文件存在并且是目录,则为真。
-e file
如果文件存在则为真。

bash test
  • 2 个回答
  • 1603 Views
Martin Hope
Dean
Asked: 2021-10-19 05:48:42 +0800 CST

如果我使用 [[ 或 [,“如果 pgrep”不再有效

  • 0

我正在尝试检测进程 ( goland.sh) 是否正在运行。我用这个:

#!/bin/bash

if pgrep "goland.sh" >/dev/null 2>&1 ; then
    echo "running"
    exit 1
fi

echo "not running"

这有效,但我不明白两件事:

  1. 为什么如果我使用if [[ pgrep "goland.sh" >/dev/null 2>&1 ]] ; then它不起作用(即使进程没有运行,它也总是打印“正在运行”)
  2. 为什么如果我使用if [ pgrep "goland.sh" >/dev/null 2>&1 ] ; then它不起作用(即使进程没有运行,它也总是打印“正在运行”)

我怀疑 1 与如何>解析有关,但我对 2 完全一无所知。

bash test
  • 1 个回答
  • 1127 Views
Martin Hope
fuumind
Asked: 2021-10-05 06:33:33 +0800 CST

bash 中 if [ ... 和 test ... 语句的区别

  • 2

考虑以下:

echo "hello" > file.txt
is_match1 () {
  local m
  m=$(cat "file.txt" | grep -F "$1")
  if [ -z "$m" ]; then
    return 1
  fi
}
is_match2 () {
  local m
  m=$(cat "file.txt" | grep -F "$1")
  test -z "$m" && return 1
}
is_match1 "hello"
echo "$?"
0
is_match2 "hello"
echo "$?"
1

为什么is_match2返回 1?

bash test
  • 1 个回答
  • 206 Views
Martin Hope
Alexander Mills
Asked: 2020-02-25 12:33:25 +0800 CST

sh(不是 bash)不识别单 [ 或双 [[ 括号?

  • 1

我有这个脚本:

#!/usr/bin/env sh

# note: we must use sh instead of bash, it's more cross-platform

set -e;

if [[ "$skip_postinstall" == "yes" ]]; then   # TODO rename 'skip_postinstall' to something more specific
    echo "skipping postinstall routine.";
    exit 0;
fi

export FORCE_COLOR=1;
export skip_postinstall="yes";   # TODO rename 'skip_postinstall' to something more specific

mkdir -p "$HOME/.oresoftware/bin" || {
  echo "Could not create .oresoftware dir in user home.";
  exit 1;
}

(
  echo 'Installing run-tsc-if on your system.';
  curl  -H 'Cache-Control: no-cache' -s -S -o- 'https://raw.githubusercontent.com/oresoftware/run-tsc-if/master/install.sh' | bash || {
     echo 'Could not install run-tsc-if on your system. That is a problem.';
     exit 1;
  }
) 2> /dev/null


if [[ "$(uname -s)" != "Darwin" ]]; then
   exit 0;
fi

if [[ ! -f "$HOME/.oresoftware/bin/realpath" ]]; then
  (
    curl --silent -o- 'https://raw.githubusercontent.com/oresoftware/realpath/master/assets/install.sh' | bash || {
       echo "Could not install realpath on your system.";
       exit 1;
    }
  )
fi

# the end of the postinstall script

当我运行它时,我得到:

./assets/postinstall.sh: 7: ./assets/postinstall.sh: [[: not found
Installing run-tsc-if on your system.

 => Installing 'run-tsc-if' on your system.
 => run-tsc-if download/installation succeeded.

./assets/postinstall.sh: 29: ./assets/postinstall.sh: [[: not found
./assets/postinstall.sh: 33: ./assets/postinstall.sh: [[: not found

但是如果我用单括号替换双 [[ 括号,我得到:

./postinstall.sh: 7: [: unexpected operator

这只是来自:

if [ "$skip_postinstall" == "yes" ]; then  
    echo "skipping postinstall routine.";
    exit 0;
fi

嗯,我该怎么办?我尝试使用test而不是[并且[[这也不起作用。

shell test
  • 2 个回答
  • 651 Views
Martin Hope
Honza Hejzl
Asked: 2019-08-07 00:57:26 +0800 CST

-a 一元运算符检查什么?

  • 1

我刚刚找到了几个使用[ -a some_dir ]or的示例,[ -a some_file ]但找不到-a运算符的用途。似乎它应该在test/的手册页中进行描述,[但它只是作为比较运算符[ $expr1 -a $expr2 ]。如果文件或目录存在,无论类型如何,它似乎都只是返回true。

bash test
  • 1 个回答
  • 120 Views
Martin Hope
user282164
Asked: 2019-06-05 10:38:58 +0800 CST

如何检查文件系统上是否存在命名管道[重复]

  • 4
这个问题在这里已经有了答案:
仅当 FIFO 存在时才写入 1 个答案
3年前关闭。

我尝试使用 -f 标志来测试是否存在命名管道

if [[ ! -f "$fifo" ]]; then
  echo 'There should be a fifo.lock file in the dir.' > /dev/stderr
  return 0;
fi

这个检查似乎不正确。所以也许命名管道不是文件,而是别的东西?

bash test
  • 1 个回答
  • 2814 Views
Martin Hope
Elegance
Asked: 2019-03-08 10:35:15 +0800 CST

测试构造中比较的退出状态

  • 3

我正在写一些“如果那么”的陈述,发现在我看来是一种奇怪的行为。经过调查,我意识到它归结为我正在进行的比较的退出代码。我在下面的代码片段中说明了我的发现。

如你看到的

rc=1
[ $rc -eq 0 ]
es_num=$?
[ $rc=0 ]
es_str=$?
echo "es_num is $es_num"
echo "es_str is $es_str"

输出

es_num is 1
es_str is 0

是否有任何文档(最好来自 POSIX 标准)讨论测试构造的退出状态-eq和=测试构造中的差异?

编写条件语句时应该注意什么?对此有哪些最佳实践?

可移植代码优于 Bash 代码(我正在使用)。

test control-flow
  • 2 个回答
  • 70 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve