mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-06 23:06:49 +02:00
Update bash help:
- refresh read command sample - fix if examples and && with ||
This commit is contained in:
@@ -138,7 +138,7 @@ to=10
|
|||||||
echo {$from..$to} # => {$from..$to}
|
echo {$from..$to} # => {$from..$to}
|
||||||
|
|
||||||
# Now that we know how to echo and use variables,
|
# Now that we know how to echo and use variables,
|
||||||
# let's learn some of the other basics of bash!
|
# let's learn some of the other basics of Bash!
|
||||||
|
|
||||||
# Our current directory is available through the command `pwd`.
|
# Our current directory is available through the command `pwd`.
|
||||||
# `pwd` stands for "print working directory".
|
# `pwd` stands for "print working directory".
|
||||||
@@ -148,33 +148,46 @@ echo "I'm in $(pwd)" # execs `pwd` and interpolates output
|
|||||||
echo "I'm in $PWD" # interpolates the variable
|
echo "I'm in $PWD" # interpolates the variable
|
||||||
|
|
||||||
# If you get too much output in your terminal, or from a script, the command
|
# If you get too much output in your terminal, or from a script, the command
|
||||||
# `clear` clears your screen
|
# `clear` clears your screen:
|
||||||
clear
|
clear
|
||||||
# Ctrl-L also works for clearing output
|
# Ctrl-L also works for clearing output.
|
||||||
|
|
||||||
# Reading a value from input:
|
# Reading a value from input:
|
||||||
echo "What's your name?"
|
echo "What's your name?"
|
||||||
read Name # Note that we didn't need to declare a new variable
|
read name
|
||||||
echo Hello, $Name!
|
# Note that we didn't need to declare a new variable.
|
||||||
|
echo "Hello, $name!"
|
||||||
|
|
||||||
# We have the usual if structure:
|
# We have the usual if structure.
|
||||||
# use `man test` for more info about conditionals
|
# Condition is true if the value of $name is not equal to the current user's login username:
|
||||||
if [ $Name != $USER ]
|
if [[ "$name" != "$USER" ]]; then
|
||||||
then
|
|
||||||
echo "Your name isn't your username"
|
echo "Your name isn't your username"
|
||||||
else
|
else
|
||||||
echo "Your name is your username"
|
echo "Your name is your username"
|
||||||
fi
|
fi
|
||||||
# True if the value of $Name is not equal to the current user's login username
|
|
||||||
|
|
||||||
# NOTE: if $Name is empty, bash sees the above condition as:
|
# To use && and || with if statements, you need multiple pairs of square brackets:
|
||||||
if [ != $USER ]
|
read age
|
||||||
# which is invalid syntax
|
if [[ "$name" == "Steve" ]] && [[ "$age" -eq 15 ]]; then
|
||||||
# so the "safe" way to use potentially empty variables in bash is:
|
echo "This will run if $name is Steve AND $age is 15."
|
||||||
if [ "$Name" != $USER ] ...
|
fi
|
||||||
# which, when $Name is empty, is seen by bash as:
|
|
||||||
if [ "" != $USER ] ...
|
if [[ "$name" == "Daniya" ]] || [[ "$name" == "Zach" ]]; then
|
||||||
# which works as expected
|
echo "This will run if $name is Daniya OR Zach."
|
||||||
|
fi
|
||||||
|
# There are other comparison operators for numbers listed below:
|
||||||
|
# -ne - not equal
|
||||||
|
# -lt - less than
|
||||||
|
# -gt - greater than
|
||||||
|
# -le - less than or equal to
|
||||||
|
# -ge - greater than or equal to
|
||||||
|
|
||||||
|
# There is also the `=~` operator, which tests a string against the Regex pattern:
|
||||||
|
email=me@example.com
|
||||||
|
if [[ "$email" =~ [a-z]+@[a-z]{2,}\.(com|net|org) ]]
|
||||||
|
then
|
||||||
|
echo "Valid email!"
|
||||||
|
fi
|
||||||
|
|
||||||
# There is also conditional execution
|
# There is also conditional execution
|
||||||
echo "Always executed" || echo "Only executed if first command fails"
|
echo "Always executed" || echo "Only executed if first command fails"
|
||||||
@@ -197,27 +210,6 @@ bg
|
|||||||
kill %2
|
kill %2
|
||||||
# %1, %2, etc. can be used for fg and bg as well
|
# %1, %2, etc. can be used for fg and bg as well
|
||||||
|
|
||||||
# To use && and || with if statements, you need multiple pairs of square brackets:
|
|
||||||
if [ "$Name" == "Steve" ] && [ "$Age" -eq 15 ]
|
|
||||||
then
|
|
||||||
echo "This will run if $Name is Steve AND $Age is 15."
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$Name" == "Daniya" ] || [ "$Name" == "Zach" ]
|
|
||||||
then
|
|
||||||
echo "This will run if $Name is Daniya OR Zach."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# There is also the `=~` operator, which tests a string against a Regex pattern:
|
|
||||||
Email=me@example.com
|
|
||||||
if [[ "$Email" =~ [a-z]+@[a-z]{2,}\.(com|net|org) ]]
|
|
||||||
then
|
|
||||||
echo "Valid email!"
|
|
||||||
fi
|
|
||||||
# Note that =~ only works within double [[ ]] square brackets,
|
|
||||||
# which are subtly different from single [ ].
|
|
||||||
# See https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs for more on this.
|
|
||||||
|
|
||||||
# Redefine command `ping` as alias to send only 5 packets
|
# Redefine command `ping` as alias to send only 5 packets
|
||||||
alias ping='ping -c 5'
|
alias ping='ping -c 5'
|
||||||
# Escape the alias and use command with this name instead
|
# Escape the alias and use command with this name instead
|
||||||
|
Reference in New Issue
Block a user