Search code examples
bashsedyad

Bash - assign variables to yad values - sed usage in for loop


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

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

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')

Two questions

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

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

EDIT: 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

Alternatives

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

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

Solution

  • It's easier than you think:

    $ values=( $(echo '7.000000 0.100000 ') )
    $ echo "${values[0]}"
    7.000000
    $ echo "${values[1]}"
    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