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 / 问题 / 1250693
Accepted
AlexP
AlexP
Asked: 2020-06-16 13:55:29 +0800 CST2020-06-16 13:55:29 +0800 CST 2020-06-16 13:55:29 +0800 CST

从bash中的for循环打印参数

  • 772

所以,我想打印满足某些条件的脚本参数:

t1=0

for i in $@; do


if [ $i == "-1" ] || [ $i == "-2" ] || [ $i == "-3" ]; then

t2=$(($t1+2))

echo $i $"$t2"

fi

t1=$(($t1+1))

done

如果我这样调用脚本:

./name_of_script  -2 aaa -m bbb -3 ccc -4 ccc -1 ddd

我希望它打印这样的东西:

-2 aaa
-3 ccc
-1 ddd

所以我想打印 -1 、 -2 或 -3 的参数,以及紧随其后的每个参数。但我得到了这个:

-2 2
-3 6
-1 10

我只是不能将 2、6 和 10 变成 $2、$6 和 $10,所以我可以打印脚本的这些参数。

有任何想法吗 ?非常感谢。

scripts command-line bash
  • 1 1 个回答
  • 140 Views

1 个回答

  • Voted
  1. Best Answer
    steeldriver
    2020-06-16T14:57:53+08:002020-06-16T14:57:53+08:00

    执行此类操作的便携式(尽管很危险)方法是通过使用eval:

    eval echo "$i \$$t2"
    

    在 bash 中,有一种更清洁、更安全的方法,它使用一种称为变量间接的特性

       If  the  first  character of parameter is an exclamation point (!), and
       parameter is not a nameref, it introduces a level of variable  indirec‐
       tion.   Bash  uses  the  value  of the variable formed from the rest of
       parameter as the name of the variable; this variable is  then  expanded
       and that value is used in the rest of the substitution, rather than the
       value of parameter itself.  This is known as  indirect  expansion.
    

    所以在你的情况下

    echo "$i ${!t2}"
    

    然而,在 shell 脚本中获取下一个位置参数的规范方法是使用shift内置函数。例如,你可以这样做

    #!/bin/bash
    
    while [ $# -gt 0 ]; do
      opt="$1"
      case $opt in
        -1|-2|-3)
          shift
          printf '%s %s\n' "$opt" "$1"
        ;;
        *)
          printf 'unknown option: %s\n' "$opt" >2 
        ;;
      esac
      shift
    done
    

    尽管如此,你似乎在这里做的是重新发明getopts内置:

    #!/bin/bash
    
    while getopts ":1:2:3:4:m:" opt; do
      case $opt in
        1|2|3)
          printf -- '-%s %s\n' "$opt" "$OPTARG"
        ;;
        *)
          printf -- 'unknown option: -%s\n' "$opt" >2 
        ;;
      esac
    done
    

    然后

    $ ./name_of_script -2 aaa -m bbb -3 ccc -4 ccc -1 ddd
    -2 aaa
    -3 ccc
    -1 ddd
    
    • 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