27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

Commands<br />

cd /hpme # Oops! Mistyped '/home'.<br />

pwd # /home<br />

# The shell corrected the misspelling.<br />

true<br />

false<br />

A command that returns a successful (zero) exit status, but does nothing else.<br />

# Endless loop<br />

while true # alias for ":"<br />

do<br />

operation−1<br />

operation−2<br />

...<br />

operation−n<br />

# Need a way to break out of loop.<br />

done<br />

A command that returns an unsuccessful exit status, but does nothing else.<br />

# Null loop<br />

while false<br />

do<br />

# The following code will not execute.<br />

operation−1<br />

operation−2<br />

...<br />

operation−n<br />

# Nothing happens!<br />

done<br />

type [cmd]<br />

Similar to the which external command, type cmd gives the full pathname to "cmd". Unlike which,<br />

type is a Bash builtin. The useful −a option to type identifies keywords and builtins, and also<br />

locates system commands with identical names.<br />

bash$ type '['<br />

[ is a shell builtin<br />

bash$ type −a '['<br />

[ is a shell builtin<br />

[ is /usr/bin/[<br />

hash [cmds]<br />

Record the path name of specified commands (in the shell hash table), so the shell or script will not<br />

need to search the $PATH on subsequent calls to those commands. When hash is called with no<br />

arguments, it simply lists the commands that have been hashed. The −r option resets the hash table.<br />

help<br />

help COMMAND looks up a short usage summary of the shell builtin COMMAND. This is the<br />

counterpart to whatis, but for builtins.<br />

bash$ help exit<br />

exit: exit [n]<br />

Exit the shell with a status of N. If N is omitted, the exit status<br />

is that of the last command executed.<br />

Chapter 11. Internal Commands and Builtins 143

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!