Chapter 7 Study Guide: Difference between revisions

From ITCwiki
Jump to navigation Jump to search
Line 133: Line 133:
Test Functions
Test Functions
-eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]
-eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]
-ne is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]
-ne is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]
-lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]
-lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]
-le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]
-le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]
-gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ]
-gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ]
-ge is greater than or equal to 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ]
 
-ge is greater than or equal to 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6  
]
string1 = string2 string1 is equal to string2
string1 = string2 string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
string1 != string2 string1 is NOT equal to string2
string1              string1 is NOT NULL or not defined  
string1              string1 is NOT NULL or not defined  
-n string1         string1 is NOT NULL and does exist
-n string1         string1 is NOT NULL and does exist
-z string1         string1 is NULL and does exist-s file  Non empty file
-z string1         string1 is NULL and does exist-s file  Non empty file
-f file  Is File exist or normal file and not a directory  
-f file  Is File exist or normal file and not a directory  
-d dir    Is Directory exist and not a file
-d dir    Is Directory exist and not a file
-w file  Is writeable file
-w file  Is writeable file
-r file  Is read-only file
-r file  Is read-only file
-x file  Is file is executable
-x file  Is file is executable
[http://www.java-samples.com/showtutorial.php?tutorialid=1345 Source]
[http://www.java-samples.com/showtutorial.php?tutorialid=1345 Source]


==Escape Sequences==
==Escape Sequences==

Revision as of 21:38, 29 February 2012

Command input and output

Bash Shell Responsible for providing user interface

Interprets commands entered on command line

Can manipulate command input and output

Input and Outputs of commands Represented by labels know as file descriptors. There are 3 file descriptors:


Standard input (stdin) information processed by the command during execution

Standard output (stdout) refers to the normal output of a command

Standard error (stderr) refers to any error messages generated by the command

Both stdout and stderr are displayed on the terminal screen by default


Use the BASH shell to redirect stdout and stderr from the terminal screen to a file on filesystem

Use the > shell metacharacter followed by absolute or relative pathname of the file

ls /etc/hosts /etc/h 1>goodoutput

Use the < to redirect a file to the stdin

cat </etc/issue

Redirection only occurs between a command and a file. However you can send stdout of 1 command to another command as stdin. You can do this by using the pipe shell metacharacter "|" This symbol can be created by pressing the shift key along with the \ key. Common use for piping is to reduce the amount of information displayed.

Shell variables

A BASH shell has several variables in memory at one time. A variable is a reserved portion of memory containing info that might be accessed. Most variables in the shell are referred to as environmental variables because they are typically set by the system and contain info that the system and programs access regularly. Users can also create custom variables. Special variables can also be used for executing commands and creating new files and directories.

Environmental Variables Many are set by default in the BASH shell. To see a list of these variables and their values use the set command. Other variables used to set the user’s work environment:

PS1 – The default shell prompt
HOME – The absolute pathname to the user’s home directory
PWD – The present working directory in the directory tree
PATH – A list of directories to search for executable programs

Home Variable Used by programs that require pathname to the current user’s home directory. If root user logs in, the HOME variable is set to /root. If user logs in, the HOME variable is set to /home/user.

SHOULD NOT BE CHANGED!

PWD Variable PWD stores the current user’s location in the directory tree. It is affected by the cd command and used by other commands, such as pwd, when the current directory need to be identified.

SHOULD NOT BE CHANGED!

Path Variable One of the most important variables in the BASH shell. It allows users to execute commands by typing the command name alone. If the executable file is in a directory not listed in the PATH variable (such as the /root directory)the user must specify either the absolute or relative pathname to the executable file.

User Defined Variables To create a variable, specify the name of the variable followed by = and the new contents. Variable identifies can contain alphanumeric characters, the "–" character, or the "_" character. They must not start with a number. They are typically capitalized to follow convention (HOME, PATH, etc.). To create a variable called MYVAR with the contents “This is a sample variable” you would use these command:

MYVAR="This is a sample variable"

Environmental Files To ensure that variables are accssible 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 are called environmental files. Some common BASH shell environment files and the order in which they are executed are:

~/.bashrc Typically used to set aliases and variables that must be present in each BASH shell. It is executed immediately after login for all users as well as when a new BASH shell is created after login

/etc/profile Always executed after login for all users on the system and sets most environmental variables

~/.bash_profile

~/.bash_login

~/.profile

Shell Scripts

A set of commands saved in a text file used to automate common tasks. Any command you can use in the command line can be used in a script. hashpling (#! /bin/bash) specifies the pathname to the shell that interprets the script. “#” by itself is a comment in the script.

Sample Script:

#!/bin/bash
#this is a comment
echo “The date is:”
date
echo “ “
echo “People logged in:”
who

'Escape Sequences Used to change output of text in the terminal.

Common ones: \??? -A 3 digit ASCII character (in octal) \\ -Backslash \a –ASCII beep \b –Backspace \c –prevents a new line between commands \n –New Line \t –Horizontal Tab \v –Vertical Tab

Read Command Saves user’s input and places it in a variable used by a script. In the example the script asks for a name, when you type it in it is saved to the NAME variable.

Echo –e “Who are you? \c”
Read NAME
Echo “Hello $NAME”

Who are you? Jason <--User's typed response

Hello Jason

Decision Constructs Not all scripts will run straight through, sometimes you need to see if something installed or the user typed something. Decision constructs allow a script to do different things in different scenarios.

Format of an If construct:

If “something is true”
Then
Do these commands
Else
Do these commands

Example:

if [ $NAME = “Jason”]
then
echo –e “Hello, Jason”
else
echo –e “Go Away”

If the variable NAME is "Jason" it will say Hello, Jason. If it is anything else it will echo "Go Away"

Test Functions -eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]

-ne is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]

-lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]

-le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]

-gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ]

-ge is greater than or equal to 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ] string1 = string2 string1 is equal to string2

string1 != string2 string1 is NOT equal to string2

string1 string1 is NOT NULL or not defined

-n string1 string1 is NOT NULL and does exist

-z string1 string1 is NULL and does exist-s file Non empty file

-f file Is File exist or normal file and not a directory

-d dir Is Directory exist and not a file

-w file Is writeable file

-r file Is read-only file

-x file Is file is executable

Source

Escape Sequences