User Tools

Site Tools


bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
bash [2023/01/17 11:24] rogerbash [2023/01/17 11:27] (current) roger
Line 13: Line 13:
   * ''-o pipefail'': This particular option sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or to zero if all commands of the pipeline exit successfully.   * ''-o pipefail'': This particular option sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or to zero if all commands of the pipeline exit successfully.
   * ''-u'': This option causes the bash shell to treat unset variables as an error and exit immediately.    * ''-u'': This option causes the bash shell to treat unset variables as an error and exit immediately. 
 +
 +More info: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
 +
 +===== Read passwords with feedback on a script =====
 +
 +<code bash>
 +unset password
 +charcount=0
 +prompt="Enter your password: "
 +while IFS= read -p "$prompt" -r -s -n 1 char
 +do
 +    if [[ $char == $'\0' ]]
 +    then
 +        break
 +    fi
 +    if [[ $char == $'\177' ]] ; then # Backspace support
 +        if [ $charcount -gt 0 ] ; then
 +            charcount=$((charcount-1))
 +            prompt=$'\b \b'
 +            password="${password%?}"
 +        else
 +            prompt=''
 +        fi
 +    else
 +        charcount=$((charcount+1))
 +        prompt='*'
 +        password+="$char"
 +    fi
 +done
 +</code>
bash.txt · Last modified: 2023/01/17 11:27 by roger