Merge pull request #365 from alemedeiros/master

[bash/en] Improved bash file
This commit is contained in:
Adam Bard 2013-09-26 20:03:56 -07:00
commit 262ea9c3ce

View File

@ -4,6 +4,7 @@ tool: bash
contributors: contributors:
- ["Max Yankov", "https://github.com/golergka"] - ["Max Yankov", "https://github.com/golergka"]
- ["Darren Lin", "https://github.com/CogBear"] - ["Darren Lin", "https://github.com/CogBear"]
- ["Alexandre Medeiros", "http://alemedeiros.sdf.org"]
filename: LearnBash.sh filename: LearnBash.sh
--- ---
@ -35,8 +36,22 @@ VARIABLE = "Some string"
# Using the variable: # Using the variable:
echo $VARIABLE echo $VARIABLE
echo "$VARIABLE" echo "$VARIABLE"
echo '$VARIABLE'
# When you use the variable itself — assign it, export it, or else — you write # When you use the variable itself — assign it, export it, or else — you write
# its name without $. If you want to use variable's value, you should use $. # its name without $. If you want to use variable's value, you should use $.
# Note that ' (single quote) won't expand the variables!
# String substitution in variables
echo ${VARIABLE/Some/A}
# This will substitute the first occurance of "Some" with "A"
# Bultin variables:
# There are some useful builtin variables, like
echo "Last program return value: $?"
echo "Script's PID: $$"
echo "Number of arguments: $#"
echo "Scripts arguments: $@"
echo "Scripts arguments separeted 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?"
@ -44,13 +59,18 @@ read NAME # Note that we didn't need to declare new variable
echo Hello, $NAME! echo Hello, $NAME!
# We have the usual if structure: # We have the usual if structure:
if true # use 'man test' for more info about conditionals
if [ $NAME -ne $USER ]
then then
echo "This is expected" echo "Your name is you username"
else else
echo "And this is not" echo "Your name isn't you username"
fi fi
# There is also conditional execution
echo "Always executed" || echo "Only executed if first command 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:
echo $(( 10 + 5 )) echo $(( 10 + 5 ))
@ -67,6 +87,13 @@ ls -l # Lists every file and directory on a separate line
# 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.
python2 hello.py < "input.in"
python2 hello.py > "output.out"
python2 hello.py 2> "error.err"
# The output error will overwrite the file if it exists, if you want to
# concatenate them, use ">>" instead.
# Commands can be substitued within other commands using $( ): # Commands can be substitued within other commands using $( ):
# The following command displays the number of files and directories in the # The following command displays the number of files and directories in the
# current directory. # current directory.
@ -80,11 +107,36 @@ 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 x y z # Note that ` ` is equivalent to $( ) and that seq returns a sequence of size 3.
for VARIABLE in `seq 3`
do do
echo "$VARIABLE" echo "$VARIABLE"
done done
# You can also define functions
# Definition:
foo ()
{
echo "Arguments work just like script arguments: $@"
echo "And: $1 $2..."
echo "This is a function"
return 0
}
# Calling your function
foo "My name is" $NAME
# There are a lot of useful commands you should learn:
tail -n 10 file.txt
# prints last 10 lines of file.txt
head -n 10 file.txt
# prints first 10 lines of file.txt
sort file.txt
# sort file.txt's lines
uniq -d file.txt
# report or omit repeated lines, with -d it reports them
cut -d ',' -f 1 file.txt
# prints only the first column before the ',' character
``` ```