IP

LINUX HACK 34

Chapter 6: Colorful and Functional Shell

Prompt Using PS1

Hack 34. Display username, hostname and basename of directory in the prompt

The PS1 in this example displays following three information in the prompt:

o \u – Username o \h – Hostname

o    \W – Base name of the current working directory


-bash-3.2$  export  PS1="\u@\h  \W>  "
ramesh@dev-db  ~>  cd  /etc/mail

ramesh@dev-db  mail>

LINUX HACK 33

Hack 33. PROMPT_COMMAND



Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.


ramesh@dev-db ~> export PROMPT_COMMAND="date +%k:%m:%S"

22:08:42 ramesh@dev-db ~>


[Note: This displays the PROMPT_COMMAND and PS1 output on different lines]


If you want to display the value of PROMPT_COMMAND in the same line as the PS1, use the echo -n as shown below.



ramesh@dev-db ~> export PROMPT_COMMAND="echo -n [$(date

+%k:%m:%S)]"


[22:08:51]ramesh@dev-db ~>


[Note: This displays the PROMPT_COMMAND and PS1 output on the same line]

LINUX HACK 32

Hack 32. PS4 - Used by “set -x” to prefix tracing output

The PS4 shell variable defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below.

Shell script and output WITHOUT PS4:

 
ramesh@dev-db  ~>  cat  ps4.sh
set  -x

echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~

ramesh@dev-db  ~>  ./ps4.sh

++ echo 'PS4 demo script' PS4 demo script
++  ls  -l  /etc/

++  wc  -l

243

++ du -sh /home/ramesh 48K /home/ramesh

[Note: This displays the default "++" while tracing the output using set -x]

Shell script and output WITH PS4:



The PS4 defined below in the ps4.sh has the following two codes: o $0 - indicates the name of script

o    $LINENO - displays the current line number within the script


ramesh@dev-db  ~>  cat  ps4.sh
export PS4='$0.$LINENO+ ' set -x

echo "PS4 demo script" ls -l /etc/ | wc -l du -sh ~

ramesh@dev-db  ~>  ./ps4.sh

../ps4.sh.3+ echo 'PS4 demo script' PS4 demo script
../ps4.sh.4+  ls  -l  /etc/
../ps4.sh.4+  wc  -l
243

../ps4.sh.5+  du  -sh  /home/ramesh

48K          /home/ramesh

[Note: This displays the modified "{script-name}.{line-number}+" while tracing the output using set -x]

LINUX HACK 31

Hack 31. PS3 - Prompt used by “select” inside shell script

You can define a custom prompt for the select loop inside a shell script, using the PS3 environment variable, as explained below.


Shell script and output WITHOUT PS3:


ramesh@dev-db ~> cat ps3.sh select i in mon tue wed exit do
case  $i  in

mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;

esac done

ramesh@dev-db ~> ./ps3.sh 1) mon
2)  tue

3)  wed

4)  exit

#?  1

Monday

#?  4

[Note: This displays the default "#?" for select command prompt]
 
Shell script and output WITH PS3:


ramesh@dev-db ~> cat ps3.sh PS3="Select a day (1-4): " select i in mon tue wed exit do
case  $i  in

mon) echo "Monday";; tue) echo "Tuesday";; wed) echo "Wednesday";; exit) exit;;

esac done

ramesh@dev-db ~> ./ps3.sh 1) mon
2)  tue

3)  wed

4)  exit

Select  a  day  (1-4):  1

Monday

Select  a  day  (1-4):  4

[Note: This displays the modified "Select a day (1-4):" for select command prompt]

LINUX HACK 30

Hack 30. PS2 - Continuation Interactive Prompt


A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-line command is “> “. Let us change this default behavior to display “continue->” by using PS2 environment variable as shown below.


ramesh@dev-db ~> myisamchk --silent --force --fast -- update-state \
>  --key_buffer_size=512M  --sort_buffer_size=512M  \

>  --read_buffer_size=4M  --write_buffer_size=4M  \

>  /var/lib/mysql/bugs/*.MYI

[Note: This uses the default ">" for continuation prompt]

ramesh@dev-db  ~>  export  PS2="continue->  "

ramesh@dev-db ~> myisamchk --silent --force --fast -- update-state \

continue-> --key_buffer_size=512M -- sort_buffer_size=512M \

continue->  --read_buffer_size=4M  --write_buffer_size=4M

\

continue->  /var/lib/mysql/bugs/*.MYI

[Note:  This  uses  the  modified  "continue->  "  for
continuation  prompt]

I found it very helpful and easy to read, when I break my long commands into multiple lines using \. I have also seen others who don’t like to break-up long commands.

LINUX HACK 29

Chapter 5: PS1, PS2, PS3, PS4 and

PROMPT_COMMAND

Hack 29. PS1 - Default Interaction Prompt


The default interactive prompt on your Linux can be modified as shown below to something useful and informative. In the following example, the default PS1 was “\s-\v\$”, which displays the shell name and the version number. Let us change this default behavior to display the username, hostname and current working directory name as shown below.


-bash-3.2$    export  PS1="\u@\h  \w>  "
ramesh@dev-db  ~>  cd  /etc/mail

ramesh@dev-db  /etc/mail>

[Note: Prompt changed to "username@hostname current-dir>" format]

Following PS1 codes are used in this example:

o    \u – Username

o    \h – Hostname

o    \w - Full pathname of current directory. Please note that when you are in the home directory, this will display only ~ as shown above


Note that there is a space at the end in the value of PS1. Personally, I prefer a space at the end of the prompt for better readability.


Make this setting permanent by adding export PS1=”\u@\h \w> ” to either

.bash_profile (or) .bashrc as shown below.
ramesh@dev-db  ~>  vi  ~/.bash_profile
ramesh@dev-db  ~>  vi  ~/.bashrc

[Note: Add export PS1="\u@\h \w> " to one of the above files]

Refer to the next chapter for several practical examples of PS1 usage in detail.


LINUX HACK 28

Hack 28. 
Display total connect time of users
Ac command will display the statistics about the user’s connect time.
Connect time for the current logged in user
With the option –d, it will break down the output for the individual days. In this example, I’ve been logged in to the system for more than 6 hours today. On Dec 1st, I was logged in for about 1 hour.
$ ac –d
Dec 1 total 1.08
Dec 2 total 0.99
Dec 3 total 3.39
Dec 4 total 4.50
Today total 6.10
 

Connect time for all the users
To display connect time for all the users use –p as shown below. Please note that this indicates the cumulative connect time for the individual users.
$ ac -p
john 3.64
madison 0.06
sanjay 88.17
nisha 105.92

ramesh 111.42
total 309.21
 

Connect time for a specific user
To get a connect time report for a specific user, execute the following:
$ ac -d sanjay
Jul 2 total 12.85
Aug 25 total 5.05
Sep 3 total 1.03
Sep 4 total 5.37
Dec 24 total 8.15
Dec 29 total 1.42
Today total 2.95


LINUX HACK 27

Hack 27. Diff Command


diff command compares two different files and reports the difference. The output is very cryptic and not straight forward to read.

Syntax:  diff  [options]  file1  file2

What was modified in my new file when compare to my old file?


The option -w in the diff command will ignore the white space while performing the comparison.


In the following diff output:

o    The lines above ---, indicates the changes happened in first file in the diff command (i.e name_list.txt).

o    The lines below ---, indicates the changes happened to the second file in the diff command (i.e name_list_new.txt). The lines that belong to the first file starts with < and the lines of second file starts with >.


#  diff  -w  name_list.txt  name_list_new.txt 
 
2c2,3
<  John  Doe

---

>  John  M  Doe

>  Jason  Bourne

LINUX HACK 26

Hack 26. Stat Command


Stat command can be used either to check the status/properties of a single file or the filesystem.

Display statistics of a file or directory.

$  stat  /etc/my.cnf





File:
`/etc/my.cnf'





Size:
346  Blocks:  16  IO  Block:  4096

regular  file
Device:
801h/2049d
Inode:  279856

Links:  1
Access:
(0644/-rw-r--r--)    Uid:  (
0/
root)
Gid:
(
0/
root)




Access:
2009-01-01  02:58:30.000000000  -0800

Modify:
2006-06-01  20:42:27.000000000  -0700

Change:
2007-02-02  14:17:27.000000000  -0800

$  stat  /home/ramesh





File:
`/home/ramesh'





Size:
4096
Blocks:  8

IO  Block:
4096
directory




Device:
803h/2051d
Inode:  5521409
Links:  7
Access:
(0755/drwxr-xr-x)    Uid:  (    401/ramesh)
Gid:  (
401/ramesh)




Access:  2009-01-01  12:17:42.000000000  -0800

Modify:  2009-01-01  12:07:33.000000000  -0800

Change:  2009-01-09  12:07:33.000000000  -0800

Display the status of the filesystem using option –f

$  stat  -f  /




File:
"/"




ID:
0
Namelen:
255
Type:  ext2/ext3
Blocks:
Total:  2579457
Free:  2008027
Available:
1876998

Size:  4096



Inodes:
Total:  1310720
Free:  1215892