[bash/en] Fix line length

This commit is contained in:
Andre Polykanine 2018-12-24 00:59:11 +02:00
parent 48c870c979
commit 4a51a5f879

View File

@ -20,21 +20,23 @@ contributors:
filename: LearnBash.sh filename: LearnBash.sh
--- ---
Bash is a name of the unix shell, which was also distributed as the shell for the GNU operating system and as default shell on Linux and Mac OS X. Bash is a name of the unix shell, which was also distributed as the shell
Nearly all examples below can be a part of a shell script or executed directly in the shell. for the GNU operating system and as default shell on Linux and Mac OS X.
Nearly all examples below can be a part of a shell script
or executed directly in the shell.
[Read more here.](http://www.gnu.org/software/bash/manual/bashref.html) [Read more here.](http://www.gnu.org/software/bash/manual/bashref.html)
```bash ```bash
#!/usr/bin/env bash #!/usr/bin/env bash
# First line of the script is shebang which tells the system how to execute # First line of the script is the shebang which tells the system how to execute
# the script: http://en.wikipedia.org/wiki/Shebang_(Unix) # the script: http://en.wikipedia.org/wiki/Shebang_(Unix)
# As you already figured, comments start with #. Shebang is also a comment. # As you already figured, comments start with #. Shebang is also a comment.
# Simple hello world example: # Simple hello world example:
echo Hello world! # => Hello world! echo Hello world! # => Hello world!
# Each command starts on a new line, or after semicolon: # Each command starts on a new line, or after a semicolon:
echo 'This is the first line'; echo 'This is the second line' echo 'This is the first line'; echo 'This is the second line'
# => This is the first line # => This is the first line
# => This is the second line # => This is the second line
@ -47,7 +49,7 @@ Variable = "Some string" # => returns error "Variable: command not found"
# Bash will decide that Variable is a command it must execute and give an error # Bash will decide that Variable is a command it must execute and give an error
# because it can't be found. # because it can't be found.
# Or like this: # Nor like this:
Variable= 'Some string' # => returns error: "Some string: command not found" Variable= 'Some string' # => returns error: "Some string: command not found"
# Bash will decide that 'Some string' is a command it must execute and give an # Bash will decide that 'Some string' is a command it must execute and give an
# error because it can't be found. (In this case the 'Variable=' part is seen # error because it can't be found. (In this case the 'Variable=' part is seen
@ -65,8 +67,9 @@ echo '$Variable' # => $Variable
# Parameter expansion ${ }: # Parameter expansion ${ }:
echo ${Variable} # => Some string echo ${Variable} # => Some string
# This is a simple usage of parameter expansion # This is a simple usage of parameter expansion
# Parameter Expansion gets a value from a variable. It "expands" or prints the value # Parameter Expansion gets a value from a variable.
# During the expansion time the value or parameter are able to be modified # It "expands" or prints the value
# During the expansion time the value or parameter can be modified
# Below are other modifications that add onto this expansion # Below are other modifications that add onto this expansion
# String substitution in variables # String substitution in variables
@ -114,8 +117,8 @@ echo {1..10} # => 1 2 3 4 5 6 7 8 9 10
echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z echo {a..z} # => a b c d e f g h i j k l m n o p q r s t u v w x y z
# This will output the range from the start value to the end value # This will output the range from the start value to the end value
# Builtin variables: # Built-in variables:
# There are some useful builtin variables, like # There are some useful built-in variables, like
echo "Last program's return value: $?" echo "Last program's return value: $?"
echo "Script's PID: $$" echo "Script's PID: $$"
echo "Number of arguments passed to script: $#" echo "Number of arguments passed to script: $#"
@ -127,7 +130,7 @@ echo "Script's arguments separated into different variables: $1 $2..."
# 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".
# We can also use the builtin variable `$PWD`. # We can also use the built-in variable `$PWD`.
# Observe that the following are equivalent: # Observe that the following are equivalent:
echo "I'm in $(pwd)" # execs `pwd` and interpolates output 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
@ -143,7 +146,7 @@ read Name # Note that we didn't need to declare a new variable
echo Hello, $Name! echo Hello, $Name!
# We have the usual if structure: # We have the usual if structure:
# use 'man test' for more info about conditionals # use `man test` for more info about conditionals
if [ $Name != $USER ] if [ $Name != $USER ]
then then
echo "Your name isn't your username" echo "Your name isn't your username"
@ -180,7 +183,7 @@ then
echo "This will run if $Name is Daniya OR Zach." echo "This will run if $Name is Daniya OR Zach."
fi fi
# There is also the =~ operator, which tests a string against a Regex pattern: # There is also the `=~` operator, which tests a string against a Regex pattern:
Email=me@example.com Email=me@example.com
if [[ "$Email" =~ [a-z]+@[a-z]{2,}\.(com|net|org) ]] if [[ "$Email" =~ [a-z]+@[a-z]{2,}\.(com|net|org) ]]
then then
@ -190,9 +193,9 @@ fi
# which are subtly different from single [ ]. # which are subtly different from single [ ].
# See http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs for more on this. # See http://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 alias and use command with this name instead # Escape the alias and use command with this name instead
\ping 192.168.1.1 \ping 192.168.1.1
# Print all aliases # Print all aliases
alias -p alias -p
@ -205,14 +208,14 @@ echo $(( 10 + 5 )) # => 15
# directory with the ls command: # directory with the ls command:
ls # Lists the files and subdirectories contained in the current directory ls # Lists the files and subdirectories contained in the current directory
# These commands have options that control their execution: # This command has options that control its execution:
ls -l # Lists every file and directory on a separate line ls -l # Lists every file and directory on a separate line
ls -t # Sorts the directory contents by last-modified date (descending) ls -t # Sorts the directory contents by last-modified date (descending)
ls -R # Recursively `ls` this directory and all of its subdirectories ls -R # Recursively `ls` this directory and all of its subdirectories
# Results of the previous command can be passed to the next command as input. # Results of the previous command can be passed to the next command as input.
# grep command filters the input with provided patterns. That's how we can list # The `grep` command filters the input with provided patterns.
# .txt files in the current directory: # That's how we can list .txt files in the current directory:
ls -l | grep "\.txt" ls -l | grep "\.txt"
# Use `cat` to print files to stdout: # Use `cat` to print files to stdout:
@ -280,10 +283,17 @@ EOF
# Run the hello.py Python script with various stdin, stdout, and # Run the hello.py Python script with various stdin, stdout, and
# stderr redirections: # stderr redirections:
python hello.py < "input.in" # pass input.in as input to the script python hello.py < "input.in" # pass input.in as input to the script
python hello.py > "output.out" # redirect output from the script to output.out python hello.py > "output.out" # redirect output from the script to output.out
python hello.py 2> "error.err" # redirect error output to error.err python hello.py 2> "error.err" # redirect error output to error.err
python hello.py > "output-and-error.log" 2>&1 # redirect both output and errors to output-and-error.log
python hello.py > /dev/null 2>&1 # redirect all output and errors to the black hole, /dev/null, i.e., no output python hello.py > "output-and-error.log" 2>&1
# redirect both output and errors to output-and-error.log
python hello.py > /dev/null 2>&1
# redirect all output and errors to the black hole, /dev/null, i.e., no output
# The output error will overwrite the file if it exists, # The output error will overwrite the file if it exists,
# if you want to append instead, use ">>": # if you want to append instead, use ">>":
python hello.py >> "output.out" 2>> "error.err" python hello.py >> "output.out" 2>> "error.err"
@ -312,11 +322,11 @@ rm -r tempDir/ # recursively delete
# current directory. # current directory.
echo "There are $(ls | wc -l) items here." echo "There are $(ls | wc -l) items here."
# The same can be done using backticks `` but they can't be nested - the preferred way # The same can be done using backticks `` but they can't be nested -
# is to use $( ). #the preferred way is to use $( ).
echo "There are `ls | wc -l` items here." echo "There are `ls | wc -l` items here."
# Bash uses a case statement that works similarly to switch in Java and C++: # Bash uses a `case` statement that works similarly to switch in Java and C++:
case "$Variable" in case "$Variable" in
#List patterns for the conditions you want to meet #List patterns for the conditions you want to meet
0) echo "There is a zero.";; 0) echo "There is a zero.";;
@ -324,7 +334,7 @@ case "$Variable" in
*) echo "It is not null.";; *) echo "It is not null.";;
esac esac
# for loops iterate for as many arguments given: # `for` loops iterate for as many arguments given:
# The contents of $Variable is printed three times. # The contents of $Variable is printed three times.
for Variable in {1..3} for Variable in {1..3}
do do
@ -345,14 +355,14 @@ done
# => 3 # => 3
# They can also be used to act on files.. # They can also be used to act on files..
# This will run the command 'cat' on file1 and file2 # This will run the command `cat` on file1 and file2
for Variable in file1 file2 for Variable in file1 file2
do do
cat "$Variable" cat "$Variable"
done done
# ..or the output from a command # ..or the output from a command
# This will cat the output from ls. # This will `cat` the output from `ls`.
for Output in $(ls) for Output in $(ls)
do do
cat "$Output" cat "$Output"
@ -432,8 +442,8 @@ grep "^foo.*bar$" file.txt | grep -v "baz"
# and not the regex, use fgrep (or grep -F) # and not the regex, use fgrep (or grep -F)
fgrep "foobar" file.txt fgrep "foobar" file.txt
# The trap command allows you to execute a command whenever your script # The `trap` command allows you to execute a command whenever your script
# receives a signal. Here, trap will execute `rm` if it receives any of the # receives a signal. Here, `trap` will execute `rm` if it receives any of the
# three listed signals. # three listed signals.
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM
@ -442,7 +452,7 @@ NAME1=$(whoami)
NAME2=$(sudo whoami) NAME2=$(sudo whoami)
echo "Was $NAME1, then became more powerful $NAME2" echo "Was $NAME1, then became more powerful $NAME2"
# Read Bash shell builtins documentation with the bash 'help' builtin: # Read Bash shell built-ins documentation with the bash `help` built-in:
help help
help help help help
help for help for
@ -450,12 +460,12 @@ help return
help source help source
help . help .
# Read Bash manpage documentation with man # Read Bash manpage documentation with `man`
apropos bash apropos bash
man 1 bash man 1 bash
man bash man bash
# Read info documentation with info (? for help) # Read info documentation with `info` (`?` for help)
apropos info | grep '^info.*(' apropos info | grep '^info.*('
man info man info
info info info info