Difference between revisions of "Script Course"
(17 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
[[File:Escape sequence.jpg|thumb|Escape Sequence]] | [[File:Escape sequence.jpg|thumb|Escape Sequence]] | ||
[[File:Test Statements.jpg|thumb|Test Statements]] | [[File:Test Statements.jpg|thumb|Test Statements]] | ||
[[File:Pipecommands.png|thumb|Commands used with pipe]] | |||
* -d File True if file is a directory | * -d File True if file is a directory | ||
* -e File True if file exists | * -e File True if file exists | ||
Line 50: | Line 52: | ||
'''Redirect both at the same time''' | '''Redirect both at the same time''' | ||
ls /etc >goodoutput 2>badoutput | ls /etc >goodoutput 2>badoutput | ||
'''tee command, which takes information from Standard Input and sends that information to a file, as well as to Standard Output.''' | |||
cat prologue|tr a A|sort|pr –d|tee newfile|less | |||
== Chaining multiple commands== | == Chaining multiple commands== | ||
cp test.txt /temp; cp test.text /tmp | cp test.txt /temp; cp test.text /tmp | ||
Line 63: | Line 68: | ||
hello Jason Dan Ryan | hello Jason Dan Ryan | ||
'''Those three names will be processed individually''' | '''Those three names will be processed individually''' | ||
===SED Command=== | |||
'''The sed command is typically used to search for a certain string of text, and replaces that text string with another text string using the syntax s/search/replace/. For example, the following output demonstrates how sed can be used to search for the string “the” and replace it with the string “THE” ''' | |||
cat filename.text | sed /s/the/THE | |||
'''Notice from the preceding output that sed only searched for and replaced the first occur- rence of the string “the” in each line. To have sed globally replace all occurrences of the string “the” in each line, simply append a g to the search-and-replace expression:''' | |||
cat prologue | sed s/the/THE/g | |||
'''You can also force sed to perform a search-and-replace on certain lines only. To replace the | |||
string “the” with “THE” globally on lines 5 to 8 only, you can use the following command: | |||
''' | |||
cat prologue | sed 5,8s/the/THE/g | |||
'''You can also use sed to remove unwanted lines of text. To delete all the lines that contain | |||
the word “the,” you can use the following command:''' | |||
cat prologue | sed /the/d | |||
===AWK Command=== | |||
'''Like sed, the awk command searches for patterns of text and performs some action on the text found. However, the awk command treats each line of text as a record in a database, and each word in a line as a database field. For example, the line “Hello, how are you?” has four fields: “Hello,” “how,” “are,” and “you?”. These fields can be referenced in the awk command using $1, $2, $3, and $4. For example, to display only the first and fourth words only on lines of the prologue file that contains the word “the,” you can use the fol- lowing command: | |||
''' | |||
cat prologue | awk ’/the/ {print $1, $4}’ | |||
find . -type f \( -name "*.nef" -o -name "*.tif" \) | awk '/cable/ {print $1}' | |||
[[File:Bashenvvariables.png|thumb|Bash Environment Variables]] | |||
'''By default, the awk command uses space or tab characters as delimiters for each field in a line. Most configuration files on Linux systems, however, are delimited using colon ( : ) char- acters. To change the delimiter that awk uses, you can specify the –F option to the com- mand. For example, the following example lists the last 10 lines of the colon-delimited file / etc/passwd and views only the 6th and 7th fields for lines that contain the word “bob” in the last 10 lines of the file''' | |||
tail /etc/passwd | awk –F : ’/bob/ {print $6, $7}’ | |||
# prints lines 1 and 7 with search term 'system' \t adds the tab between fields | |||
cat /etc/passwd | awk -F : '/system/ {print $1, "\t"$7' | |||
# prints dir contents with links and p tags | |||
ls | awk -F: '{print "<p> <a href=\""$1 "\">"$1 "</a></p>"}' > index.html | |||
===List Environment Variables=== | |||
'''Many environment variables are set by default in the BASH shell. To see a list of these vari- ables and their current values, you can use the set command, ''' | |||
set | less | |||
===Setting User Variables=== | |||
*They can contain alphanumeric characters (0–9, A–Z, a–z), the dash (-) character, or the underscore (_) character. | |||
*They must not start with a number. | |||
*They are typically capitalized to follow convention (e.g., HOME, PATH | |||
MYVAR = "hello" | |||
echo $MYVAR | |||
Use the '''export''' command to make a variable available to all sub-shells | |||
export MYVAR | |||
#env command is used to view user variables | |||
env |grep MYVAR | |||
===Environment Files=== | |||
Recall that variables are stored in memory. When a user exits the BASH shell, all variables stored in memory are destroyed along with the shell itself. To ensure that variables are acces- sible to a shell at all times, you must place variables in a file that is executed each time a user logs in and starts a BASH shell. These files are called environment files. Common BASH shell environment files and the order in which they are typically executed are as follows: | |||
*/etc/profile | |||
*/etc/bashrc | |||
*~/.bashrc | |||
*~/.bash_profile | |||
*~/.bash_login | |||
*~/.profile | |||
==If Statements== | |||
<pre> | |||
echo –e "\nThe people logged into the system include:" | |||
who | |||
echo –e "\nWould you like to see the contents of /?(y/n) --> \c" | |||
read ANSWER | |||
if [ $ANSWER = "y" –o $ANSWER = "Y" ] | |||
then | |||
echo –e "\nThe contents of the / directory are:" | |||
ls –F / | |||
fi | |||
</pre> | |||
==Select Case== | |||
<pre> | |||
#!/bin/bash | |||
echo –e "What would you like to see? | |||
Todays date (d) | |||
Currently logged in users (u) | |||
The contents of the / directory (r) | |||
Enter your choice(d/u/r)--> \c" | |||
read ANSWER | |||
case $ANSWER in d | D ) echo -e "\nToday’s date is: \c" | |||
date ;; | |||
u | U ) echo –e "\nThe people logged into the system include:" | |||
who | |||
;; | |||
r | R ) echo –e "\nThe contents of the / directory are:" ls –F / | |||
;; *) echo –e "Invalid choice! \a" | |||
;; | |||
esac | |||
</pre> | |||
=[[Bash| Bash menu]]-[[Main_Page| Home]]= | =[[Bash| Bash menu]]-[[Main_Page| Home]]= | ||
[[Category:Bash]] |
Latest revision as of 17:12, 14 November 2019
File Operators Tests
- -d File True if file is a directory
- -e File True if file exists
- -f File True if file exists and is a regular file
- -r File True if file is readable by you
- -s File True if file exist and is not empty
- -w File True if the file is writable by you
- -x File True if the file is executable by you
- -z String True if string is empty
- -n String true if string is not empty
Syntax
[condition to test for]
Example
[ -e /etc/passwd ] -e True if file exists
Exit Status return codes
- Every command returns an exit status
- range from 0 - 255
- 0 = success
- Other than 0 = error condition
- use for error checking
$? Contains the return code of the previously executed command
ls /not/here echo "$?"
-
HOST = "google.com" ping -c 1 $HOST if [ "$?" -eq "0" ] then echo "$HOST reachable." else echo "$HOST unreachable" fi
' And and || or
- && = And : if first fails the other won't be executed
mkdir /tmp/bak && cp test.txt /tmp/bak/
- || OR - if one fails the other will be processed
cp test.txt /tmp/bak || cp test.txt /tmp
Redirection
redirect to non error using status code 1 to filename goodoutput
ls /etc 1>goodoutput ls /etc > 2>badoutput
Redirect both at the same time
ls /etc >goodoutput 2>badoutput
tee command, which takes information from Standard Input and sends that information to a file, as well as to Standard Output.
cat prologue|tr a A|sort|pr –d|tee newfile|less
Chaining multiple commands
cp test.txt /temp; cp test.text /tmp
Bash Functions
#!/bin/bash function hello(){ for NAME in $@ do echo "Hello $NAME" done }
Call Script
hello Jason Dan Ryan
Those three names will be processed individually
SED Command
The sed command is typically used to search for a certain string of text, and replaces that text string with another text string using the syntax s/search/replace/. For example, the following output demonstrates how sed can be used to search for the string “the” and replace it with the string “THE”
cat filename.text | sed /s/the/THE
Notice from the preceding output that sed only searched for and replaced the first occur- rence of the string “the” in each line. To have sed globally replace all occurrences of the string “the” in each line, simply append a g to the search-and-replace expression:
cat prologue | sed s/the/THE/g
You can also force sed to perform a search-and-replace on certain lines only. To replace the string “the” with “THE” globally on lines 5 to 8 only, you can use the following command:
cat prologue | sed 5,8s/the/THE/g
You can also use sed to remove unwanted lines of text. To delete all the lines that contain the word “the,” you can use the following command:
cat prologue | sed /the/d
AWK Command
Like sed, the awk command searches for patterns of text and performs some action on the text found. However, the awk command treats each line of text as a record in a database, and each word in a line as a database field. For example, the line “Hello, how are you?” has four fields: “Hello,” “how,” “are,” and “you?”. These fields can be referenced in the awk command using $1, $2, $3, and $4. For example, to display only the first and fourth words only on lines of the prologue file that contains the word “the,” you can use the fol- lowing command:
cat prologue | awk ’/the/ {print $1, $4}’
find . -type f \( -name "*.nef" -o -name "*.tif" \) | awk '/cable/ {print $1}'
By default, the awk command uses space or tab characters as delimiters for each field in a line. Most configuration files on Linux systems, however, are delimited using colon ( : ) char- acters. To change the delimiter that awk uses, you can specify the –F option to the com- mand. For example, the following example lists the last 10 lines of the colon-delimited file / etc/passwd and views only the 6th and 7th fields for lines that contain the word “bob” in the last 10 lines of the file
tail /etc/passwd | awk –F : ’/bob/ {print $6, $7}’ # prints lines 1 and 7 with search term 'system' \t adds the tab between fields cat /etc/passwd | awk -F : '/system/ {print $1, "\t"$7' # prints dir contents with links and p tags
ls | awk -F: '{print "
<a href=\""$1 "\">"$1 "</a>
"}' > index.html
List Environment Variables
Many environment variables are set by default in the BASH shell. To see a list of these vari- ables and their current values, you can use the set command,
set | less
Setting User Variables
- They can contain alphanumeric characters (0–9, A–Z, a–z), the dash (-) character, or the underscore (_) character.
- They must not start with a number.
- They are typically capitalized to follow convention (e.g., HOME, PATH
MYVAR = "hello" echo $MYVAR
Use the export command to make a variable available to all sub-shells
export MYVAR #env command is used to view user variables env |grep MYVAR
Environment Files
Recall that variables are stored in memory. When a user exits the BASH shell, all variables stored in memory are destroyed along with the shell itself. To ensure that variables are acces- sible to a shell at all times, you must place variables in a file that is executed each time a user logs in and starts a BASH shell. These files are called environment files. Common BASH shell environment files and the order in which they are typically executed are as follows:
- /etc/profile
- /etc/bashrc
- ~/.bashrc
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
If Statements
echo –e "\nThe people logged into the system include:" who echo –e "\nWould you like to see the contents of /?(y/n) --> \c" read ANSWER if [ $ANSWER = "y" –o $ANSWER = "Y" ] then echo –e "\nThe contents of the / directory are:" ls –F / fi
Select Case
#!/bin/bash echo –e "What would you like to see? Todays date (d) Currently logged in users (u) The contents of the / directory (r) Enter your choice(d/u/r)--> \c" read ANSWER case $ANSWER in d | D ) echo -e "\nToday’s date is: \c" date ;; u | U ) echo –e "\nThe people logged into the system include:" who ;; r | R ) echo –e "\nThe contents of the / directory are:" ls –F / ;; *) echo –e "Invalid choice! \a" ;; esac