Standard input , output, error

stdin represented by the number 0, stdout represented by the number 1, and stderr represented by the number 2.

Redirection:  > shell metacharacter followed by the absolute or relative pathname of the file.

[root@server1 ~]# ls /etc/hosts /etc/h 2>badoutput
/etc/hosts
[root@server1 ~]# cat badoutput
ls: cannot access /etc/h: No such file or directory

Because the error was redirected into the file “badoutput”, so only standard input was shown on the screen.

Append: >> followed by the absolute or relative pathname of the file.

TR

Tr command is one such command that can be used to replace characters in a file sent via Standard Input.

To translate all of the lowercase
r characters in the /etc/issue file to uppercase R characters, you can run the following command:
[root@server1 ~]# tr r R </etc/issue

Pipe (|)

Send the Standard Output of one command to another command as Standard Input.

Options may be used with pipe:

  • sort: sort the lines according to the first letter of the line.
  • nl: number each line
  • tee: Takes information from Standard Input and sends that information to a file.
  • awk: searches for patterns of text and performs some action on the text found.
  • sed: is typically used to search for a certain string of text, and replaces that text string with another text string.
Sed

Search a text and manipulate it.

Syntax: sed s/search/replace/.

Note that this only replace the first occurrence, replace all occurrences of the string “the” in each line, simply append a g.

such as sed s/search/replace/g .

1. certain lines only. To replace the string “the” with “THE” globally on lines 5 to 10 only, you can use the following command:
[root@server1 ~]# cat prologue | sed 5,10s/the/THE/g

2. replace the string “the” with “THE” globally on lines that contain the string “love,” you can use the following command:
[root@server1 ~]# cat prologue | sed /love/s/the/THE/g

3. delete all the lines that contain the word “the,” you can use the following command:
[root@server1 ~]# cat prologue | sed /the/d

Variables

Set: see a list of these variables and their current values.

echo: see a specific variable and its value, specify the variable name prefixed by the $ .

[root@server1 ~]# echo $PS1
[\u@\h \W]\$
[root@server1 ~]# _

Note that a special notation is used to define the prompt in the preceding output: \u indicates the user name, \h indicates the host name, and \W indicates the name of the current
directory.

= : used to give a variable value.

Subshell:

A subshell is a separate instance of the command process. The subshell is created by parentheses.

Most commands that are run by the shell are run in a separate subshell, which is created by the current shell. Any variables created in the current shell are not available to those subshells.

Demo:

Create a script with name subshelltest:

#vi  subshelltest

var1=23
echo “$var1”

( var1=76;

echo “————”
echo “I am inside the subshell”
echo “$var1”

echo “————”
)
echo “$var1”

#chmod +x  subshelltest

#./subshelltest

23

————
I am inside the subshell
76

————
23

 

Export command to ensure that all programs started by the current shell have the ability to access the variable.

we still use the script created above, but we do some change here:

var1=23
echo “$var1”
export var1

(
echo “————”
echo “I am inside the subshell”
echo “$var1”
echo “————”
)
echo “$var1”

Then the result becomes:

 

# ./subshelltest
23
————
I am inside the subshell
23
————
23

 

Another example is use another script in one script.

in my.sh:

#! /bin/bash
echo “hello world”

myvar=”another one”

./my2.sh

in my2.sh:

#!/bin/bash
echo $myvar

then execute the command;

root@frank:~/Documents# ./my.sh proxylist.txt result.txt
hello world

Notice that the last line does not show the content of variable myvar.

add this line into the last line of my.sh:export myvar

Then re-execute the command:

root@frank:~/Documents# ./my.sh proxylist.txt result.txt
hello world
another one

PATH environment variable

The PATH is an environment variable. It is a colon delimited list of directories that your shell searches through when you enter a command. All executables are kept in different directories on the Linux and Unix like operating systems.

Finding out your current path

To find out what your current path setting, type the following command at shell prompt. Open the Terminal and then enter:

echo "$PATH"

OR

printf "%s\n" "$PATH"

Sample outputs:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/sbin/modemZapp:/Users/vivek/gcutil-1.8.4
How do I modify my path?

To modify your path edit $PATH variable as per your shell. The syntax for setting path under UNIX / Linux dependent upon your login shell.

Bash, Sh, Ksh shell syntax to modify $PATH

If you are using bash, sh, or ksh, at the shell prompt, type:

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
export PATH=$PATH:/path/to/dir1
export PATH=$PATH:/path/to/dir1:/path/to/dir2

OR

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
PATH=$PATH:/path/to/dir1; export PATH

Please feel free to replace /path/to/dir1 with the directory you want the shell to search.

Tcsh or csh shell syntax to modify $PATH

If you are using tcsh or csh, shell enter:

 ## please note 'path' is case sensitivity and must be in lowercase ##
set path = ($path /path/to/dir1)
set path = ($path /path/to/dir1 /path/to/dir2)

OR

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
setenv PATH $PATH:/path/to/dir1
setenv PATH $PATH:/path/to/dir1:/path/to/dir2

Please feel free to replace /path/to/dir1 with the directory you want the shell to search.

Examples

In this example add /usr/local/bin to your path under BASH/ksh/sh shell, enter:

export PATH=$PATH:/usr/local/bin

OR

PATH=$PATH:/usr/local/bin; export PATH

To make these changes permanent, add the commands described above to the end of your ~/.profile file for sh and ksh shell, or ~/.bash_profile file for bash shell:

## BASH SHELL ##
echo 'export PATH=$PATH:/usr/local/bin'  >> ~/.bash_profile

KSH/sh shell user try:

## KSH / SH SHELL ##
echo 'export PATH=$PATH:/usr/local/bin'  >> ~/.profile

In this final example add /usr/local/bin/ and /scripts/admin/ to your path under csh / tcsh shell, enter:

set path = ($path /usr/local/bin /scripts/admin)

OR

setenv PATH $PATH:/usr/local/bin:/scripts/admin

To make these changes permanent, add the commands described above to the end of your ~/.cshrc file:

echo 'set path = ($path /usr/local/bin /scripts/admin)'  >> ~/.cshrc

OR

echo 'setenv PATH $PATH:/usr/local/bin:/scripts/admin'  >> ~/.cshrc

To verify new path settings, enter:
$ echo $PATH

LAB reflection
  • tr: only replace the single character with a new one, when you use it to replace the whole words, unexpected result will happen. For example:
[root@localhost Documents]# cat test
hello Bob,
This is a test letter for my lab test.
[root@localhost Documents]# tr test TEST < test
hEllo Bob,
ThiS iS a TEST lETTEr for my lab TEST.

It basically replace the t with T, e with E, s with S, t with T. So if you want to replace test with TESt, probably you won’t get the result,

[root@localhost Documents]# tr test TESt < test
hEllo Bob,
ThiS iS a tESt lEttEr for my lab tESt.