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
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1518193
Accepted
stackbiz
stackbiz
Asked: 2024-06-20 21:06:03 +0800 CST2024-06-20 21:06:03 +0800 CST 2024-06-20 21:06:03 +0800 CST

如何在 Bash 数组中使用星号 (*)

  • 772

我想将所有内容存储command args在一个中array,但它只有当所有参数都没有任何(*)时才有效。

这是一个不使用数组的测试:

mkdir -p /tmp/hello
echo "hello" > /tmp/hello/hello.txt
echo "world" > /tmp/hello/world.txt

mkdir -p /tmp/world

# This cp command is success
cp /tmp/hello/* /tmp/world

现在将 cp 命令转换为 my_array:

mkdir -p /tmp/hello
echo "hello" > /tmp/hello/hello.txt
echo "world" > /tmp/hello/world.txt

mkdir -p /tmp/world

declare -a my_array=()
my_array[0]="cp"
my_array[1]="/tmp/hello/*"
my_array[2]="/tmp/world"

# Run the command and it will fail
"${my_array[@]}"

错误如下:

cp: cannot stat '/tmp/hello/*': No such file or directory

*在 中可以使用 ( ) 吗?使用 实现 ' ' 的my_array正确语法是什么?cp /tmp/hello/* /tmp/worldmy_array

更新:

有一个problem来自@choroba's answoer,$count和$sedond_item将是错误的:

mkdir -p /tmp/hello
echo "hello" > /tmp/hello/hello.txt
echo "world" > /tmp/hello/world.txt

mkdir -p /tmp/world

my_array=(cp)
my_array+=(/tmp/hello/*)
my_array+=(/tmp/world)

count=${#my_array[@]}
printf "%s\n" "count is: $count"

sedond_item="${my_array[1]}"
printf "%s\n" "second item is: $sedond_item"

以下是@choroba 回答的输出:

count is: 4
second item is: /tmp/hello/hello.txt

但在我的原始数组中$count和是正确的:$sedond_item

mkdir -p /tmp/hello
echo "hello" > /tmp/hello/hello.txt
echo "world" > /tmp/hello/world.txt

mkdir -p /tmp/world

declare -a my_array=()
my_array[0]="cp"
my_array[1]="/tmp/hello/*"
my_array[2]="/tmp/world"

count=${#my_array[@]}
printf "%s\n" "count is: $count"

sedond_item="${my_array[1]}"
printf "%s\n" "second item is: $sedond_item"

这是原始数组的输出:

count is: 3
second item is: /tmp/hello/*
command-line
  • 2 2 个回答
  • 119 Views

2 个回答

  • Voted
  1. Best Answer
    steeldriver
    2024-06-21T01:53:37+08:002024-06-21T01:53:37+08:00

    我认为你不能安全地在bash 中做你想做的事。

    在 bash(以及 POSIX sh 和 ksh)中,参数扩展(包括数组扩展,如"{my_array[@]}"支持它们的 shell 中的数组扩展)的双引号可防止单词拆分和文件名生成(也称为 globbing)。另一方面,未加引号的扩展既要进行单词拆分,又要进行 globbing(所谓的“split+glob”操作)。在 bash 中,您可以使用set -f(或等效地)关闭 globbing,set -o noglob但据我所知,您不能在保持 globbing开启的情况下关闭单词拆分。

    请注意,单词拆分发生在通配符之前/path/without/whitespace/*,因此问题不在于的扩展/path/without/whitespace/name with whitespace,而在于 的扩展/path with whitespace/*。

    然而,在 zsh 中,未加引号的变量扩展默认不受 split+glob 约束,您可以使用~参数扩展标志2单独启用 globbing 。

    请注意,在所有情况下,即使取消引用,var=*或之类的赋值语句的 RHS也不会扩展。my_array[1]=/tmp/hello/*

    因此以 zsh 为例(注意 zsh 数组从 1 开始索引,而不是像 bash 那样从 0 开始):

     % typeset -a my_array
     % my_array[1]=ls
     % my_array[2]="my dir/*"
     % typeset -p my_array
    typeset -a my_array=( ls 'my dir/*' )
    
     % ${my_array[@]}
    ls: cannot access 'my dir/*': No such file or directory
    

    "${my_array[@]}"(与 bash 和 zsh 中引用的相同)但是

     % ${~my_array[@]}
    'my dir/hello'  'my dir/world'
    

    您可以通过添加另一个文件来验证扩展时是否发生了通配符:

     % touch 'my dir/a new file'
    steeldriver@steeldriver-virtualbox ~/dir
     % ${~my_array[@]}          
    'my dir/a new file'  'my dir/hello'  'my dir/world'
    

    对于您的具体情况,我建议添加一些安全措施 - 特别是使用 GNU mv-t明确设置目标目录并-n防止意外覆盖,以及一个附加参数--来标记选项的结束(这在 glob 可能扩展为以连字符开头的文件名时变得很重要):

    #!/bin/zsh
    
    typeset -a my_array
    
    my_array[1]=mv
    my_array[2]=-nt
    my_array[3]=/tmp/world
    my_array[4]=--
    my_array[5]=/tmp/hello/*
    
    ${~my_array[@]}
    

    尽管老实说,如果你要切换到 zsh,你可能会考虑使用贡献zmv模块 - 它采用(引用的)glob 表达式并在内部扩展它们 - 代替普通的mv。


    1. 我的意思是不使用eval

    2. 但请注意,在数组的 每个${~my_array[@]}元素扩展期间都会启用通配符

    • 2
  2. choroba
    2024-06-20T21:23:03+08:002024-06-20T21:23:03+08:00

    填充数组时使用星号,但不要一次填充一个元素:

    my_array=(cp /tmp/hello/* /tmp/world)
    "${my_array[@]}"
    

    或者使用+=运算符扩展数组:

    my_array=(cp)
    my_array+=(/tmp/hello/*)
    my_array+=(/tmp/world)
    "${my_array[@]}"
    
    • 1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

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

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve