2

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/^........//')
GeorgeC
  • 81
  • 2
  • 9
  • 1
    I don't know what `yad` is or does, but `radius=$(sed 's|[amount*,]||g')` doesn't specify what data to operate on, so it defaults to wait forever for you to enter data via the keyboard. You can use `radius=$(echo "$i" | sed 's|[amount*,]||g')` to feed it some data so it doesn't wait for you to enter it manually. I don't know what you expect this `sed` to do though, and I'm guessing it won't do it. – that other guy Aug 19 '18 at 02:07
  • Thanks. Of course... sed needs data to work with. The for loop was from another post and is not necessary. Answer below. – GeorgeC Aug 19 '18 at 03:25
  • yad is a graphical interface for bash scripting – GeorgeC Aug 19 '18 at 18:54
  • As an aside, the regex `[amount,*]` almost certainly doesn't do what you want or believe it does. It matches a single character which is not (newline or) `a` or `m` or `o` or `u` or `n` or `t` or `,` or `*`. Probably you should replace the square brackets with round ones, though you will then need to use `sed -r` or `sed -E` or understand how to further modify the expression for your particular `sed` version's regex dialect. – tripleee Aug 20 '18 at 09:04
  • Thanks. I see that now... this has been a very helpful post, all round. The patience and time spent by the responders is much appreciated. – GeorgeC Aug 20 '18 at 20:37

2 Answers2

0

EDIT: Changed answer based on @Ed Morton

#!/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'))

if [ $? -eq 0 ]; then
    radius="${values[0]}"
    amount="${values[1]}"
fi
exit

bash -x Output

+ '[' 0 -eq 0 ']'
+ radius=7.000000
+ amount=1.000000
+ exit
GeorgeC
  • 81
  • 2
  • 9
  • You don't need echo and sed for that - `radius="${values%????????}"; amount="${values#????????}"`. What are you hoping that `echo " " >/dev/null 2>&1` will do? It does nothing useful as far as I can tell. What do you think `if [ $? = 1 ]` is testing? If you think it's the result of `yad` - it's not. – Ed Morton Aug 19 '18 at 13:11
  • Btw it's extremely likely that this task would be a bit simpler if you DID have a separator but you never showed us what the output from `yad` with a separator looked like... – Ed Morton Aug 19 '18 at 13:30
  • @Ed Morton. Thanks - I've learned something from both responders - added the separator and your method works well - changed answer to include it and added the bash -x output. I will have to think on your comments about testing yad output and the option of skipping the process. – GeorgeC Aug 19 '18 at 18:53
  • You're welcome. Your test for success is still in the wrong place, assuming you want to test the result of executing `yad` rather than the result of `amount=...` and you still have that echo > /dev/null that does nothing. I'll post an answer. – Ed Morton Aug 19 '18 at 19:04
  • 1
    That's hit the mark - "Replace $(echo '7.000000 0.100000 ') with yad". It should have occurred to me to define the variables, as you have - an array? Then having checked for non zero values the script prints the values or, otherwise, terminates / moves on to the next process. If this is an accurate summary? – GeorgeC Aug 19 '18 at 23:05
  • thats about it but to be precise it's checking for a success exist status, not non-zero values. – Ed Morton Aug 20 '18 at 00:40
  • For what it's worth, you should almost never need to examine `$?` directly. Anything which looks like `commands ...; if [ $? -eq 0 ]; then` can be more idiomatically and elegantly rewritten `if commands ...; then` – tripleee Aug 21 '18 at 12:54
  • Possibly another question... I am getting my head around this. The examples followed elsewhere on-line, mainly applied to exit codes for yad (1 and 252) use the if [ $? = 1 ]; then echo " "; else apply the yad values – GeorgeC Aug 22 '18 at 01:42
  • Cont... Based on the examples provided here, bash exits the block if there is nothing to process. I am having difficulty understanding the differences @tripleeee – GeorgeC Aug 22 '18 at 01:58
  • He's just saying you can write `if date; then echo succ; fi` rather than `date; if [ $? -eq 0 ]; then echo succ; fi`. The benefit to the former is you can't accidentally add code between `date` and `if` (as actually happened in your code in your question). The way that form of the code is usually written is `date; ret=$?` all on one line and then `if [ $ret -eq 0 ]; then echo succ; fi` afterwards to avoid that potential problem by saving the return code immediately upon the command executing. – Ed Morton Aug 22 '18 at 11:24
  • An `if` in bash is exactly the same as an `if` in any other language - if the condition is true then execute the subsequent code, otherwise don't. Bash (or any other language) doesn't exit the block when the condition is false, it just never enters the block. – Ed Morton Aug 22 '18 at 11:30
0

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185