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
Next revision
Previous revision
bash [2023/01/17 14:24] rogerbash [2024/11/17 12:59] (current) – external edit 127.0.0.1
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.1673965462.txt.gz · Last modified: 2024/11/17 12:59 (external edit)