从列表中读取值并在shell脚本中的命令行中写入

时间:2016-04-25 08:06:47

标签: shell unix

在从列表中读取值并将其作为输入写入下一个命令时需要一些帮助。

我需要编写一个需要

的shell脚本
  1. v1.0 v2.0 v3.0 v4.0 ---列出所有标签或版本号,如:

    read v3.0
  2. 我需要读取列表输出中的一个值,例如v3.0并将其传递给下一个命令

  3. git checkout <version number>

  4. git checkout v3.0示例:{{1}}
  5. 我如何在shell脚本中实现它?请帮忙

1 个答案:

答案 0 :(得分:0)

阅读你的上一条评论,我认为@fedorqui是对的。这个问题是重复的:你想建立一个菜单并提示用户。在您的情况下,您应该使用options=( $(git tag -l) )

List options in menu in bash处的脚本变为:

#!/bin/bash

prompt="Please select a file:"
options=( $(git tag -l) )

PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do 
    if (( REPLY == 1 + ${#options[@]} )) ; then
        exit

    elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
        echo  "You picked $opt which is file $REPLY"
        break

    else
        echo "Invalid option. Try another one."
    fi
done    

git checkout $opt

----

历史的第一个答案。

您要查找的命令是xargs

git tag -l | fgrep v3.0 | xargs -I xxx git checkout xxx

我必须提醒您,如果多个标记包含v3.0,那么git checkout将被执行多次。

相关问题