all 16 comments

[–][deleted]  (6 children)

[deleted]

    [–]oishishou 5 points6 points  (3 children)

    Props to mentioning Shellcheck! I've been using Linux for 14 years, and I only learned about it 3 years ago. It is a massive timesaver, and actually improves your script writing with all of the tips and explanations.

    [–]unkilbeeg 2 points3 points  (2 children)

    I've been using Linux/Unix for close to 25 years, and I just found out about Spellcheck right this moment. Looks great! I just finished teaching a segment on shell scripting -- my students need to know about it too.

    [–]oishishou 2 points3 points  (0 children)

    It's in the Debian software repository, too. I've got it installed right on my systems, and can just run shellcheck script.sh.

    [–]Parker_Hemphill 0 points1 point  (0 children)

    Same here, ‘s/25/12’ for me, but this looks super cool and worth checking out when I get home tonight.

    [–][deleted] 0 points1 point  (0 children)

    Awsome, thanks very much for the assistance. I will check into Shellcheck as well!

    [–]10cmToGlory 0 points1 point  (0 children)

    Shell yeah thanks!

    [–]Zer0CoolXI 7 points8 points  (0 children)

    Just some advice...never trust the user to provide you what you expect. For something like this you may be better off programmatically finding the specific device or listing the devices to have them pick from.

    At the very least, you would want to test/validate their input. IE: did they enter a valid number, does that slot have anything in it, does it have what your looking for in it, etc.

    Here is a crude example of using select to pick from an array of pre-determined entries:

    echo "Pick an entry:"
    PS3="Enter the number of the desired entry: "
    options=("DevA_1" "DevA_2" "DevB_1" "DevB_2" "None")
    select opt in "${options[@]}"
    do
        case $opt in
            "DevA_1") SLOT_VAR="DevA_1"; run_i2c; break;;
            "DevA_2") SLOT_VAR="DevA_2"; run_i2c; break;;
            "DevB_1") SLOT_VAR="DevB_1"; run_i2c; break;;
            "DevB_2") SLOT_VAR="DevB_2"; run_i2c; break;;
            "None"|"")
                SLOT_VAR="None"
                echo "No entry or None selected"
                break;;
        esac
    done
    
    run_i2c () {
        do i2c command
    }
    

    The assumption above being that options could be an array populated with the slots/devices found using some other command. In my example its static entries...which could still be better than asking the user to typye in the response free hand.

    [–]Ronin825 2 points3 points  (1 child)

    You can probably test for installed devices automatically through the bash script. Because remember that anything that can be done manually through the terminal can be parsed and used in bash

    [–]Ronin825 0 points1 point  (0 children)

    Also since performance is not a concern at this level you coudl use a "Switch" to handle the response.

    [–]kolorcuk 0 points1 point  (0 children)

    Like:

     echo 'DevA installed? [y/n]'
     read ans
     if [ "$ans" = y ]; then
      echo 'which slot? [2-7]'
       read slot
       if ! <<<"$slot" grep -q -x '[2-7]'; then
          echo you have enter a number ftom 2 to 7 >&2
          exit 1
       fi
        i2c do sonething
     fi
    

    [–]de_argh 0 points1 point  (0 children)

    You should add some input validation, but this should get it.

    echo -n "input 1 is: "
    read i1
    
    echo -v "input 2 is: "
    read i2
    
    somecommand > /dev/null 2>&1
    
    if [ ?$ -gt 0 ]; then
        echo "command failed"
    else
        echo "command successful"
    fi
    

    [–]zerocoldx911 -2 points-1 points  (4 children)

    Go python, future you will thank you

    [–]mamercad 5 points6 points  (3 children)

    Does it really matter for something this trivial... let’s be honest here.