Bash-将变量分配给yad值-for循环中的sed用法 [英] Bash - assign variables to yad values - sed usage in for loop

查看:45
本文介绍了Bash-将变量分配给yad值-for循环中的sed用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我尝试将变量分配给两个yad值Radius和Amount.

In the code below I am attempting to assign variables to the two yad values Radius and Amount.

这可以通过将yad值打印到文件中来用awk完成,但我想避免这种情况.

This can be done with awk by printing the yad values to file but I want to avoid this if I can.

为字符串(即两个yad值)分配了一个变量,并根据需要使用sed修剪了字符.但是,脚本在此行停止.

The string (that is, both yad values) is assigned a variable and trimmed of characters, as required, using sed. However, the script stops at this line;

radius=$(sed 's|[amount*,]||g')

两个问题

  1. 有没有更好的方法来解决这个问题?和

  1. is there a better way of tackling this; and

为什么脚本未完成?我无法弄清楚语法.

why is the script not completing? I have not been able to figure out the syntax.

不需要循环并使用sed语法

don't need the loop and working on the sed syntax

  #!/bin/bash
    #ifs.sh

        values=`yad --form --center --width=300 --title="Test" --separator=' ' \
            --button=Skip:1 \
            --button=Apply:0 \
            --field="Radius":NUM \
                '0!0..30!1!0' \
            --field="Amount":NUM \
                '0!0..5!0.01!2'`

            radius=$(echo "$values" | sed 's|[amount*,]||g')
            amount=$(echo "$values" | sed 's/.a://')

            if [ $? = 1 ]; then
            echo " " >/dev/null 2>&1; else

            echo "Radius = $radius"
            echo "Amount = $amount"
        fi
    exit

替代品

# with separator
#  radius="${values%????????}"
#  amount="${values#????????}"

# without separator
#   radius=$(echo "$values" | sed s'/........$//')
#   amount=$(echo "$values" | sed 's/^........//')

推荐答案

比您想象的要容易:

$ values=( $(echo '7.000000 0.100000 ') )
$ echo "${values[0]}"
7.000000
$ echo "${values[1]}"
0.100000

yad ... 替换 $(echo'7.000000 0.100000'),因此脚本应为:

Replace $(echo '7.000000 0.100000 ') with yad ... so the script would be:

values=( $(yad --form --center --width=300 --title="Test" --separator=' ' \
        --button=Skip:1 \
        --button=Apply:0 \
        --field="Radius":NUM \
            '0!0..30!1!0' \
        --field="Amount":NUM \
            '0!0..5!0.01!2') )

if [ $? -eq 0 ]; then
    echo "Radius = ${values[0]}"
    echo "Amount = ${values[1]}"
fi

这篇关于Bash-将变量分配给yad值-for循环中的sed用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆