mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-06 14:58:31 +00:00
commit
79cc976ab4
@ -21,7 +21,7 @@ Nearly all examples below can be a part of a shell script or executed directly i
|
|||||||
# 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!
|
echo Hello world!
|
||||||
|
|
||||||
# Each command starts on a new line, or after semicolon:
|
# Each command starts on a new line, or after 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'
|
||||||
@ -56,24 +56,24 @@ echo "Last program return value: $?"
|
|||||||
echo "Script's PID: $$"
|
echo "Script's PID: $$"
|
||||||
echo "Number of arguments: $#"
|
echo "Number of arguments: $#"
|
||||||
echo "Scripts arguments: $@"
|
echo "Scripts arguments: $@"
|
||||||
echo "Scripts arguments separeted in different variables: $1 $2..."
|
echo "Scripts arguments seperated in different variables: $1 $2..."
|
||||||
|
|
||||||
# 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 new variable
|
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 -ne $USER ]
|
if [ $NAME -ne $USER ]
|
||||||
then
|
then
|
||||||
echo "Your name is you username"
|
echo "Your name is your username"
|
||||||
else
|
else
|
||||||
echo "Your name isn't you username"
|
echo "Your name isn't your username"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# There is also conditional execution
|
# There is also conditional execution
|
||||||
echo "Always executed" || echo "Only executed if first command fail"
|
echo "Always executed" || echo "Only executed if first command fails"
|
||||||
echo "Always executed" && echo "Only executed if first command does NOT fail"
|
echo "Always executed" && echo "Only executed if first command does NOT fail"
|
||||||
|
|
||||||
# Expressions are denoted with the following format:
|
# Expressions are denoted with the following format:
|
||||||
@ -81,7 +81,7 @@ echo $(( 10 + 5 ))
|
|||||||
|
|
||||||
# Unlike other programming languages, bash is a shell — so it works in a context
|
# Unlike other programming languages, bash is a shell — so it works in a context
|
||||||
# of current directory. You can list files and directories in the current
|
# of current directory. You can list files and directories in the current
|
||||||
# directories with ls command:
|
# directory with the ls command:
|
||||||
ls
|
ls
|
||||||
|
|
||||||
# These commands have options that control their execution:
|
# These commands have options that control their execution:
|
||||||
@ -89,10 +89,10 @@ ls -l # Lists every file and directory on a separate line
|
|||||||
|
|
||||||
# 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
|
# grep command filters the input with provided patterns. That's how we can list
|
||||||
# txt files in the current directory:
|
# .txt files in the current directory:
|
||||||
ls -l | grep "\.txt"
|
ls -l | grep "\.txt"
|
||||||
|
|
||||||
# You can also redirect a command output, input and error output.
|
# You can also redirect a command, input and error output.
|
||||||
python2 hello.py < "input.in"
|
python2 hello.py < "input.in"
|
||||||
python2 hello.py > "output.out"
|
python2 hello.py > "output.out"
|
||||||
python2 hello.py 2> "error.err"
|
python2 hello.py 2> "error.err"
|
||||||
@ -116,7 +116,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 var $VARIABLE is printed three times.
|
# The contents of var $VARIABLE is printed three times.
|
||||||
for VARIABLE in {1..3}
|
for VARIABLE in {1..3}
|
||||||
do
|
do
|
||||||
|
Loading…
Reference in New Issue
Block a user