Softwares |
Main /
BASHMain.BASH HistoryHide minor edits - Show changes to output Added lines 16-28:
Example 2 Conditional Branching =========================== [notroot@puppet ~]$ if test "$USER" != 'root' ; then > echo "you are not logged in as root" > fi you are not logged in as root [notroot@puppet ~]$ if test "$USER" != 'root' ; then echo "you are not logged in as root"; fi you are not logged in as root [notroot@puppet ~]$ Deleted lines 5-6:
Changed lines 16-18 from:
to:
@] !! test - check file types and compare values Added lines 5-19:
!!Example Conditional Branching: ========================== [@ root is in ! ~/patch read -p "Enter 1 or 0: " ; if test "$1" = 0;then echo 1; else echo 0; fi Enter 1 or 0: 1 0 root is in ! ~/patch read -p "Enter 1 or 0: " ; if test "$1" = 0;then echo 1; else echo 0; fi Enter 1 or 0: 0 0 root is in ! ~/patch Changed lines 173-174 from:
Values passed in from the command line as arguments are accessed as $# where #= the index to:
Values passed in from the command line as arguments are accessed as $# where #= the index of the variable in the array of values being passed in. This array is base 1 not base 0. command var1 var2 var3 .... varX $1 contains whatever var1 was, $2 contains whatever var2 was, etc. Added line 183:
Added line 185:
Added line 187:
Added line 189:
Changed lines 195-200 from:
Regular double quotes ("like these") make the shell ignore whitespace and count it all as one argument being passed or string to use. Special characters inside are still noticed/obeyed. Single quotes 'like this' make the interpreting shell ignore all special characters in whatever string is being passed. The back single quote marks (`command`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls program are put in the variable contents. to:
Regular double quotes ("like these") make the shell ignore whitespace and count it all as one argument being passed or string to use. Special characters inside are still noticed/obeyed. Single quotes 'like this' make the interpreting shell ignore all special characters in whatever string is being passed. The back single quote marks (`command`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls program are put in the variable contents. Changed lines 214-215 from:
A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the entrance/exit criteria for a loop. to:
A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the entrance/exit criteria for a loop. Changed lines 311-312 from:
string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings. to:
string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings. Changed lines 326-331 from:
This executes once for each item in the list. This list can be a variable that contains several words separated by spaces (such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned the current item in the list, until the last one is reached. to:
This executes once for each item in the list. This list can be a variable that contains several words separated by spaces (such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned the current item in the list, until the last one is reached. Deleted lines 148-149:
(:cellnr:) Changed lines 152-153 from:
All bash scripts must tell the o/s what to use as the interpreter. The first line of any script should be: to:
All bash scripts must tell the o/s what to use as the interpreter. The first line of any script should be: Changed lines 164-166 from:
Create a variable - just assign value. Variables are non-datatyped (a variable can hold strings, numbers, etc. with out being defined as such). to:
Create a variable - just assign value. Variables are non-datatyped (a variable can hold strings, numbers, etc. with out being defined as such). Changed lines 173-177 from:
Values passed in from the command line as arguments are accessed as $# where #= the index of the variable in the array of values being passed in. This array is base 1 not base 0. $1 contains whatever var1 was, $2 contains whatever var2 was, etc. to:
Values passed in from the command line as arguments are accessed as $# where #= the index of the variable in the array of values being passed in. This array is base 1 not base 0. command var1 var2 var3 .... varX $1 contains whatever var1 was, $2 contains whatever var2 was, etc. Added lines 176-177:
================== Changed lines 185-188 from:
Regular double quotes ("like these") make the shell ignore whitespace and count it all as one argument being passed or string to use. Special characters inside are still noticed/obeyed. to:
=========== Regular double quotes ("like these") make the shell ignore whitespace and count it all as one argument being passed or string to use. Special characters inside are still noticed/obeyed. Changed lines 191-195 from:
The back single quote marks (`command`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls program are put in the variable contents. to:
The back single quote marks (`command`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls program are put in the variable contents. Changed lines 194-197 from:
A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the entrance/exit criteria for a loop. to:
===================== A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the entrance/exit criteria for a loop. Changed line 203 from:
to:
==================== Added lines 212-213:
================== Added line 217:
Added line 221:
Added lines 233-234:
====================== Deleted line 235:
Added line 237:
Deleted line 238:
Added line 240:
Deleted line 241:
Changed lines 247-248 from:
to:
============ Changed lines 268-269 from:
to:
========================= Changed lines 281-282 from:
to:
=========== Changed lines 292-296 from:
string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings. to:
string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings. Changed lines 295-296 from:
to:
================= Changed lines 302-306 from:
This executes once for each item in the list. This list can be a variable that contains several words separated by spaces (such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned the current item in the list, until the last one is reached. to:
This executes once for each item in the list. This list can be a variable that contains several words separated by spaces (such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned the current item in the list, until the last one is reached. Changed lines 315-316 from:
to:
========= Deleted lines 334-336:
Changed line 40 from:
-n STRING to:
-n STRING Changed lines 76-77 from:
FILE1 -ot FILE2 to:
FILE1 is newer than FILE2 FILE1 -ot FILE2 Changed lines 112-113 from:
FILE exists and is owned by the effective user ID to:
FILE exists and is owned by the effective user ID Changed lines 144-145 from:
to:
Learning about bash scripting. Deleted line 1:
Changed lines 5-7 from:
A quick cheat sheet for programmers who want to do shell scripting. This is not intended to teach programming, etc. but it is intended for a someone who knows one programming language to begin learning about bash scripting. to:
[@ NAME test - check file types and compare values SYNOPSIS test EXPRESSION test [ EXPRESSION ] [ ] [ OPTION DESCRIPTION Exit with the status determined by EXPRESSION. --help display this help and exit --version output version information and exit An omitted EXPRESSION defaults to false. , EXPRESSION is true or false and sets exit status. It is one of: ( EXPRESSION ) EXPRESSION is true ! EXPRESSION EXPRESSION is false EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true -n STRING the length of STRING is nonzero STRING equivalent to -n STRING -z STRING the length of STRING is zero STRING1 = STRING2 the strings are equal STRING1 != STRING2 the strings are not equal INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers FILE1 -nt FILE2 FILE1 -ot FILE2 FILE1 is older than FILE2 -b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -g FILE FILE exists and is set-group-ID -G FILE FILE exists and is owned by the effective group ID -h FILE FILE exists and is a symbolic link (same as -L) -k FILE FILE exists and has its sticky bit set -L FILE FILE exists and is a symbolic link (same as -h) -O FILE FILE exists and is owned by the effective user ID -p FILE FILE exists and is a named pipe -r FILE FILE exists and read permission is granted -s FILE FILE exists and has a size greater than zero -S FILE FILE exists and is a socket -t FD file descriptor FD is opened on a terminal -u FILE FILE exists and its set-user-ID bit is set -w FILE FILE exists and write permission is granted -x FILE FILE exists and execute (or search) permission is granted @] Written By: ph34r - http://www.linux-sxs.org/programming/bashcheat.html A quick cheat sheet for programmers who want to do shell scripting. This is not intended to teach programming, etc. but it is intended for a someone who knows one programming language to begin learning about bash scripting. Changed lines 7-8 from:
A quick cheat sheet for programmers who want to do shell scripting. This is not intended to teach programming, etc. but it is intended for a someone who knows one programming language to begin learning about bash scripting. to:
A quick cheat sheet for programmers who want to do shell scripting. This is not intended to teach programming, etc. but it is intended for a someone who knows one programming language to begin learning about bash scripting. Changed lines 27-28 from:
Create a variable - just assign value. Variables are non-datatyped (a variable can hold strings, numbers, etc. with out being defined as such). to:
Create a variable - just assign value. Variables are non-datatyped (a variable can hold strings, numbers, etc. with out being defined as such). Changed lines 36-37 from:
Values passed in from the command line as arguments are accessed as $# where #= the index of the variable in the array of values being passed in. This array is base 1 not base 0. to:
Values passed in from the command line as arguments are accessed as $# where #= the index of the variable in the array of values being passed in. This array is base 1 not base 0. Changed lines 50-51 from:
Regular double quotes ("like these") make the shell ignore whitespace and count it all as one argument being passed or string to use. Special characters inside are still noticed/obeyed. to:
Regular double quotes ("like these") make the shell ignore whitespace and count it all as one argument being passed or string to use. Special characters inside are still noticed/obeyed. Changed lines 55-56 from:
The back single quote marks (`command`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls to:
The back single quote marks (`command`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls program are put in the variable contents. Changed lines 62-63 from:
A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the entrance/exit criteria for a loop. to:
A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the entrance/exit criteria for a loop. Changed lines 151-152 from:
string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings. to:
string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings. Changed lines 163-166 from:
This executes once for each item in the list. This list can be a variable that contains several words separated by spaces (such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned the current item in the list, until the last one is reached. to:
This executes once for each item in the list. This list can be a variable that contains several words separated by spaces (such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned the current item in the list, until the last one is reached. Added lines 1-189:
!! Bash Programming Cheat Sheet Written By: ph34r - http://www.linux-sxs.org/programming/bashcheat.html (:Googletxt:) (:Google1:) ---- A quick cheat sheet for programmers who want to do shell scripting. This is not intended to teach programming, etc. but it is intended for a someone who knows one programming language to begin learning about bash scripting. !!Basics (:table border=1 width=50% cellspacing=0 :) (:cellnr:) [@ All bash scripts must tell the o/s what to use as the interpreter. The first line of any script should be: #!/bin/bash You must make bash scripts executable. chmod +x filename Variables Create a variable - just assign value. Variables are non-datatyped (a variable can hold strings, numbers, etc. with out being defined as such). varname=value Access a variable by putting $ on the front of the name echo $varname Values passed in from the command line as arguments are accessed as $# where #= the index of the variable in the array of values being passed in. This array is base 1 not base 0. command var1 var2 var3 .... varX $1 contains whatever var1 was, $2 contains whatever var2 was, etc. Built in variables: $1-$N Stores the arguments (variables) that were passed to the shell program from the command line. $? Stores the exit value of the last command that was executed. $0 Stores the first word of the entered command (the name of the shell program). $* Stores all the arguments that were entered on the command line ($1 $2 ...). "$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...). Quote Marks Regular double quotes ("like these") make the shell ignore whitespace and count it all as one argument being passed or string to use. Special characters inside are still noticed/obeyed. Single quotes 'like this' make the interpreting shell ignore all special characters in whatever string is being passed. The back single quote marks (`command`) perform a different function. They are used when you want to use the results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls program are put in the variable contents. Logic and comparisons A command called test is used to evaluate conditional expressions, such as a if-then statement that checks the entrance/exit criteria for a loop. test expression Or [ expression ] Numeric Comparisons int1 -eq int2 Returns True if int1 is equal to int2. int1 -ge int2 Returns True if int1 is greater than or equal to int2. int1 -gt int2 Returns True if int1 is greater than int2. int1 -le int2 Returns True if int1 is less than or equal to int2 int1 -lt int2 Returns True if int1 is less than int2 int1 -ne int2 Returns True if int1 is not equal to int2 String Comparisons str1 = str2 Returns True if str1 is identical to str2. str1 != str2 Returns True if str1 is not identical to str2. str Returns True if str is not null. -n str Returns True if the length of str is greater than zero. -z str Returns True if the length of str is equal to zero. (zero is different than null) File Comparisons -d filename Returns True if file, filename is a directory. -f filename Returns True if file, filename is an ordinary file. -r filename Returns True if file, filename can be read by the process. -s filename Returns True if file, filename has a nonzero length. -w filename Returns True if file, filename can be written by the process. -x filename Returns True if file, filename is executable. Expression Comparisons !expression Returns true if expression is not true expr1 -a expr2 Returns True if expr1 and expr2 are true. ( && , and ) expr1 -o expr2 Returns True if expr1 or expr2 is true. ( ||, or ) Logic Con't. If...then if [ expression ] then commands fi If..then...else if [ expression ] then commands else commands fi If..then...else If...else if [ expression ] then commands elif [ expression2 ] then commands else commands fi Case select case string1 in str1) commands;; str2) commands;; *) commands;; esac string1 is compared to str1 and str2. If one of these strings matches string1, the commands up until the double semicolon (; ;) are executed. If neither str1 nor str2 matches string1, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings. Iteration (Loops) for var1 in list do commands done This executes once for each item in the list. This list can be a variable that contains several words separated by spaces (such as output from ls or cat), or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned the current item in the list, until the last one is reached. while [ expression ] do commands done until [ expression ] do commands done Functions Create a function: fname(){ commands } Call it by using the following syntax: fname Or, create a function that accepts arguments: fname2 (arg1,arg2...argN){ commands } And call it with: fname2 arg1 arg2 ... argN @] (:tableend:) ---- (:Googlemm:) |