From c7a1136772588e57a3f9c20be9102a80a6f7f3e8 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 27 Aug 2013 21:23:09 -0700 Subject: Edits --- bash.html.markdown | 2 -- 1 file changed, 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 7421f880..a0c43c12 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -1,12 +1,10 @@ --- - category: tool tool: bash contributors: - ["Max Yankov", "https://github.com/golergka"] - ["Darren Lin", "https://github.com/CogBear"] 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. -- cgit v1.2.3 From e2cd18ba463279a650c2393f68f37131a52b4c4d Mon Sep 17 00:00:00 2001 From: Avjinder Date: Sat, 31 Aug 2013 17:05:04 +0530 Subject: Update bash.html.markdown In the case statement, the "in" keyword should be on the same line as case $VARIABLE. Also, ;; should be present at the end of each command. Shell executes all statements up to the two semicolons that are next to each other. --- bash.html.markdown | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index a0c43c12..76c794c6 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -73,12 +73,11 @@ ls -l | grep "\.txt" echo "There are $(ls | wc -l) items here." # Bash uses a case statement that works similarily to switch in Java and C++: -case "$VARIABLE" -in +case "$VARIABLE" in #List patterns for the conditions you want to meet - 0) echo "There is a zero." - 1) echo "There is a one." - *) echo "It is not null." + 0) echo "There is a zero.";; + 1) echo "There is a one.";; + *) echo "It is not null.";; esac #For loops iterate for as many arguments given: -- cgit v1.2.3 From 1528dd4fbe680f26646be3af17d9fb5dce209dd0 Mon Sep 17 00:00:00 2001 From: JohnYangSam Date: Thu, 12 Sep 2013 02:01:52 -0700 Subject: Correct bash for...in loop example Removed the $ in the variable declaration of the for...in bash loop to correct the code. --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 76c794c6..708131bd 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -82,7 +82,7 @@ esac #For loops iterate for as many arguments given: #The contents of var $VARIABLE is printed three times. -for $VARIABLE in x y z +for VARIABLE in x y z do echo "$VARIABLE" done -- cgit v1.2.3 From c14586e8c3c285b34c20358fa97e44aeed356dde Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 01:06:35 -0300 Subject: add conditional execution info to bash --- bash.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 708131bd..41d0669e 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -4,6 +4,7 @@ tool: bash contributors: - ["Max Yankov", "https://github.com/golergka"] - ["Darren Lin", "https://github.com/CogBear"] + - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] filename: LearnBash.sh --- @@ -51,6 +52,10 @@ else echo "And this is not" 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: echo $(( 10 + 5 )) -- cgit v1.2.3 From 9d0f731ad5c6e3e9ccda43651eb4f445fc8234f0 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 01:29:05 -0300 Subject: Improve bash variable info --- bash.html.markdown | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 41d0669e..81565b6d 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -36,8 +36,22 @@ VARIABLE = "Some string" # Using the variable: echo $VARIABLE echo "$VARIABLE" +echo '$VARIABLE' # 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 $. +# 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: echo "What's your name?" -- cgit v1.2.3 From 9c8c0af0af1caef57f463c7c41e56ccc76f414d6 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:04:24 -0300 Subject: add information about input, output, and error redirection --- bash.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 81565b6d..f281c1eb 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -86,6 +86,13 @@ ls -l # Lists every file and directory on a separate line # txt files in the current directory: 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 $( ): # The following command displays the number of files and directories in the # current directory. -- cgit v1.2.3 From 96055ac7a513fef69fd4323df465537b8df835c4 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:09:28 -0300 Subject: better for description --- bash.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index f281c1eb..fe17e710 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -106,9 +106,10 @@ case "$VARIABLE" in *) echo "It is not null.";; esac -#For loops iterate for as many arguments given: -#The contents of var $VARIABLE is printed three times. -for VARIABLE in x y z +# For loops iterate for as many arguments given: +# The contents of var $VARIABLE is printed three times. +# Note that ` ` is equivalent to $( ) and that seq returns a sequence of size 3. +for VARIABLE in `seq 3` do echo "$VARIABLE" done -- cgit v1.2.3 From e1c34ca138cc1a978a85ba5062bed47219ac2d1c Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:20:10 -0300 Subject: improve if on bash --- bash.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index fe17e710..4a290358 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -59,11 +59,12 @@ read NAME # Note that we didn't need to declare new variable echo Hello, $NAME! # We have the usual if structure: -if true +# use 'man test' for more info about conditionals +if [ $NAME -ne $USER ] then - echo "This is expected" + echo "Your name is you username" else - echo "And this is not" + echo "Your name isn't you username" fi # There is also conditional execution -- cgit v1.2.3 From 182eab60517e571931d7b928c28fb88a88fca894 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:28:59 -0300 Subject: add function information for bash --- bash.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 4a290358..ed93d58b 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -115,4 +115,16 @@ do echo "$VARIABLE" 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 ``` -- cgit v1.2.3 From b3288dc9cd0e1bd39e0e6d41664b120ea2f2e02a Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:35:04 -0300 Subject: add useful commands --- bash.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index ed93d58b..4d80545e 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -127,4 +127,16 @@ foo () # 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 ``` -- cgit v1.2.3 From b2028c7f0c3087b34b601ab27e61cc59ae1e9a21 Mon Sep 17 00:00:00 2001 From: Christos Kontas Date: Fri, 4 Oct 2013 18:32:11 +0300 Subject: [bash] Fix some spell errors in comments --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 4d80545e..276bc31f 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -94,12 +94,12 @@ 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 substituted within other commands using $( ): # The following command displays the number of files and directories in the # current directory. echo "There are $(ls | wc -l) items here." -# Bash uses a case statement that works similarily to switch in Java and C++: +# Bash uses a case statement that works similarly to switch in Java and C++: case "$VARIABLE" in #List patterns for the conditions you want to meet 0) echo "There is a zero.";; -- cgit v1.2.3 From 3bfc820721618af1c1421e2858198bbc739ec70a Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 13 Oct 2013 00:14:24 -0300 Subject: add another way to define functions to bash (fix #380) --- bash.html.markdown | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 276bc31f..d208b957 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -117,7 +117,7 @@ done # You can also define functions # Definition: -foo () +function foo () { echo "Arguments work just like script arguments: $@" echo "And: $1 $2..." @@ -125,6 +125,13 @@ foo () return 0 } +# or simply +bar () +{ + echo "Another way to declare functions!" + return 0 +} + # Calling your function foo "My name is" $NAME -- cgit v1.2.3