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 / 问题

问题[bash-expansion](unix)

Martin Hope
Herosław Miraszewski
Asked: 2019-06-06 22:36:56 +0800 CST

为什么 $() 中的命令打印的 {1,2} 没有被插值?

  • 8

我在一个目录中,其中有两个文本文件:

$ touch test1.txt
$ touch test2.txt

当我尝试使用某种模式列出文件(使用 Bash)时,它可以工作:

$ ls test?.txt
test1.txt  test2.txt
$ ls test{1,2}.txt
test1.txt  test2.txt

然而,当一个模式由一个包含在 中的命令产生时$(),只有一个模式有效:

$ ls $(echo 'test?.txt')
test1.txt  test2.txt
$ ls $(echo 'test{1,2}.txt')
ls: cannot access test{1,2}.txt: No such file or directory

这里发生了什么?为什么模式{1,2}不起作用?

bash bash-expansion
  • 5 个回答
  • 1146 Views
Martin Hope
conanDrum
Asked: 2019-05-24 02:15:20 +0800 CST

Bash 替换显示目录结构

  • -1

我想在将变量写入文件之前转义变量中的引号。不幸的是,其中一些行有一些不愉快的项目。

(我想避免 foo 扩展。我希望 foo="ModPagespeedLoadFromFileRuleMatch disallow .*" 从字面上理解并在其上运行替换。我不想要任何扩展。它只是放置在文件中的指令。)

foo="ModPagespeedLoadFromFileRuleMatch disallow .*"
echo ${foo//\\/\\\\}

这给了我:

ModPagespeedLoadFromFileRuleMatch disallow . .. .bash_history .bashrc .cloud-locale-test.skip .filemin .gnupg .local .mysql_history .profile .rnd .rpmdb .ssh

我也试过这样:

foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( printf "$foo" | sed 's/\\/\\\\/g' );
echo $bar

foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( echo "$foo" | sed 's/\\/\\\\/g' );
echo $bar

同样的问题。

我在这里想念什么?

PS。尝试这个:

foo='ModPagespeedLoadFromFileRuleMatch disallow .*' 
echo "$foo" 

然后试试这个(我不希望在管道时发生这种情况):

foo='ModPagespeedLoadFromFileRuleMatch disallow .*' 
echo "$foo" | 

注意第二个例子中的管道

回答:

foo="ModPagespeedLoadFromFileRuleMatch\ disallow .*"
echo "${foo//\\/\\\\}"

foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( printf "$foo" | sed 's/\\/\\\\/g' )
echo "$bar"

foo='ModPagespeedLoadFromFileRuleMatch disallow .*';
bar=$( echo "$foo" | sed 's/\\/\\\\/g' );
echo "$bar"
bash bash-expansion
  • 1 个回答
  • 52 Views
Martin Hope
Tim
Asked: 2019-03-22 15:09:10 +0800 CST

`~#` 是 bash 的波浪号扩展吗?

  • 4

是~#bash 的波浪号扩展吗?我在https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html中找不到它。我的问题来自https://unix.stackexchange.com/a/506532/674。谢谢。

$ ~#
The following connections are open:
  #0 client-session (t4 r0 i0/0 o0/0 fd 5/6 cc -1)
  #1 x11 (t4 r3 i0/0 o0/0 fd 8/8 cc -1)
bash bash-expansion
  • 1 个回答
  • 126 Views
Martin Hope
JdeHaan
Asked: 2019-03-21 01:14:35 +0800 CST

Bash 脚本在命令行工作的地方失败

  • 0

我编写了一个基本的 bash 函数来测试另一个字符串中是否出现字符串搜索参数。从命令行运行它会产生预期的结果。从 shell 脚本运行相同失败。有人可以告诉我我的方式的错误吗?

me@mylaptop $ line='someidentifier 123                     another identifier 789 065             theend'
me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
me@mylaptop $ type strstr
strstr is a function
strstr ()
{
   [ "${1#*$2*}" = "$1" ] && return 1;
   return 0
}
me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
0
me@mylaptop $

我的脚本:

me@mylaptop $ cat l.sh
#!/bin/bash

function strstr ()
{
   [ "${1#*$2*}" = "$1" ] && return 1;
   return 0
}

line='someidentifier 123                     another identifier 789 065             theend'
sarg='+([0-9])+([ ])another identifier'

echo '==='"$line"'==='

echo '==='"$sarg"'==='

strstr "$line" "$sarg"

echo "$?"

me@mylaptop $ ./l.sh
===someidentifier 123                     another identifier 789 065             theend===
===+([0-9])+([ ])another identifier===
1
me@mylaptop $
bash bash-expansion
  • 1 个回答
  • 145 Views
Martin Hope
argle
Asked: 2019-02-22 01:50:16 +0800 CST

$@ 是否有标准的数组名称,例如 ${ARGS[@]}?

  • 0

我不希望它是可修改的。也就是说,我不希望它启用类似的东西ARGS=("${ARGS[@]}"),而只是简化类似的东西myarray=("${ARGS[@]/--unneeded-argument/}")。

请注意,该--unneeded-argument示例并不意味着删除参数是我问的原因。这是我多年来一直在思考的问题,但我从未得到明确的答案。

bash bash-expansion
  • 2 个回答
  • 70 Views
Martin Hope
Bret Joseph
Asked: 2018-12-14 02:19:32 +0800 CST

如何使用括号进行扩展,但在输出中使用逗号而不是空格作为分隔符?

  • 2

我需要这样做IFS=",";echo {1..5}才能输出1,2,3,4,5而不是1 2 3 4 5. 如何制作 bash echo {1..5} 并用逗号输出值?

bash-expansion
  • 5 个回答
  • 981 Views
Martin Hope
ottotts
Asked: 2018-11-16 04:24:08 +0800 CST

限制 Bash 文件名扩展

  • 0

为了将文件写入 USB 卷,我想重命名包含 '?'、'"'、'*' 或 ':' 的文件名,以用空格替换这些字符中的任何一个。但是我在使用 '* '. 我的 bash 脚本是

for file in * 
do 
    mv -v "'"$file"'" "'"$(echo "$file" | sed 's/\(.*\)[?"*:]\(.*\)/\1 \2/')"'" 
done

'*' 不断扩展到当前目录中的文件名中。我试过使用

set -f

但是最初的“in *”不起作用。

bash bash-expansion
  • 2 个回答
  • 119 Views
Martin Hope
NarūnasK
Asked: 2018-09-05 12:03:26 +0800 CST

使用 bash 变量间接访问时如何访问数组的更多成员?

  • 2

考虑以下示例,它似乎与 index 一起工作正常0:

$ a1=(1 2 3)
$ a2=(a b c)
$ for x in a1 a2; do echo "${!x}"; done
1
a
$ for x in a1 a2; do echo "${!x[0]}"; done
1
a

但是对于索引1,它什么也不打印:

$ for x in a1 a2; do echo "${!x[1]}"; done    

数组本身就很好:

$ echo "${a1[1]} ${a2[1]}"
2 b

编辑 - 基于ilkkachu答案的现实生活用例

SHIBB=(https://shibboleth.net/downloads/service-provider/3.0.2/ shibboleth-sp-3.0.2 .tar.gz)
XERCES=(http://apache.mirrors.nublue.co.uk//xerces/c/3/sources/ xerces-c-3.2.1 .tar.gz)
XMLSEC=(http://apache.mirror.anlx.net/santuario/c-library/ xml-security-c-2.0.1 .tar.gz)
XMLTOOL=(http://shibboleth.net/downloads/c++-opensaml/latest/ xmltooling-3.0.2 .tar.gz)
OPENSAML=(http://shibboleth.net/downloads/c++-opensaml/latest/ opensaml-3.0.0 .tar.gz)

typeset -n x
for x in XERCES XMLSEC XMLTOOL OPENSAML SHIBB; do
  url="${x[0]}" app="${x[1]}" ext="${x[2]}"
  [ -f "./${app}${ext}" ] || wget "${url}${app}${ext}"
  tar -xf "./${app}${ext}"
  cd "./${app}" && ./configure && make -j2 && make install && ldconfig
  cd ..
done
bash bash-expansion
  • 1 个回答
  • 88 Views
Martin Hope
mf395
Asked: 2018-07-14 12:14:49 +0800 CST

-q 内部 Makefile 赋值

  • 0

正在检查包含以下内容的 Makefile:

ifdef REALLY_QUIET
  export REALLY_QUIET
  ECHO := true
  LFLAGS := $(LFLAGS) -q
  YFLAGS := $(YFLAGS) -q
endif

您能否指出最后两个作业 (:=) 中 -q 项的含义是什么?我检查了https://www.gnu.org/software/make/manual/make.html#MAKE-Variable但我认为那里提到的 -q 或 --question 是针对“make”命令而不是针对内容的生成文件。

谢谢!

make bash-expansion
  • 1 个回答
  • 69 Views
Martin Hope
Tim
Asked: 2018-05-16 14:49:40 +0800 CST

展开一个变量,其值包含一个选项和一个带空格的路径名

  • 0
$ md="-l /tmp/test/my dir"
$ ls "$md"
ls: invalid option -- ' '
Try 'ls --help' for more information.

$ md="-l \"/tmp/test/my dir\""
$ ls "$md"
ls: invalid option -- ' '
Try 'ls --help' for more information.

我想知道为什么两者都不起作用?谢谢。注意变量的值包含

  • 一个选项-l
  • 包含空格的路径名参数
  • 分隔上述两者的空间。

我的实际问题是:我写了一个脚本myscript.sh

#! /bin/bash

old_dest=""
while getopts ":l:t" opt; do
    case $opt in
        l)
            old_dest="--link-dest \"$OPTARG\""
            ;;
    esac
done

rsync -a --delete "$old_dest" /path/to/source  /path/to/dest

我想将脚本作为带有-l选项的命令调用。

myscript.sh -l "/media/t/my external hdd/backup/old one"

我应该如何编写分配给的脚本部分old_dest?

bash bash-expansion
  • 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