bash
Last modified: December 2, 2006
This page refers to bash - navigation and scripting.
Navigation
CTRL Key Bound
Ctrl + a - Jump to the start of the line Ctrl + e - Jump to the end of the line Ctrl + b - Move back a char Ctrl + f - Move forward a char Ctrl + k - cut to EOL, and then Ctrl + Y is yank (or paste) what you cut previously. Ctrl + u - Delete backward from cursor. Ctrl + w - Delete word by word in reverse. Ctr + t - swapping of the last two chars. A common mistake while typing. !$ = last argument of last command, for example: user@system:mkdir /this/is/a/dir user@system:cd !$ Esc-f - move forward by word. Esc-b - move backward by word.
More Special Keybindings
Here “2T” means Press TAB twice
$ 2T - All available commands(common) $ (string)2T - All available commands starting with (string) $ /2T - Entire directory structure including Hidden one $ 2T - Only Sub Dirs inside including Hidden one $ *2T - Only Sub Dirs inside without Hidden one $ ~2T - All Present Users on system from "/etc/passwd" $ $2T - All Sys variables $ @2T - Entries from "/etc/hosts" $ =2T - Output like ls or dir
Scripting
A complete guide is available here.
A simple for loop..
#!/bin/sh
for x in `ipcs | cut -f 2 -d ' '`
do
ipcrm -q $x
done
This script tries to remove any msg queues listed with ipcs.
Another example, this time with an if condition..
if [ ! -e "$file" ] # Check if file exists.
then
echo "$file does not exist."; echo
continue # On to next.
fi
Test if file exists:
if [ -f .bash_profile ]; then
echo "You have a .bash_profile. Things are fine."
else
echo "Yikes! You have no .bash_profile!"
fi
Reading text from stdin:
echo -n "Enter some text > " read text echo "You entered: $text"
And now with a timeout of 3 seconds:
echo -n "Hurry up and type something! > "
if read -t 3 response; then
echo "Great, you made it in time!"
else
echo "Sorry, you are too slow!"
fi
Kind of a simple menu:
echo -n "Enter a number between 1 and 3 inclusive > "
read character
if [ "$character" = "1" ]; then
echo "You entered one."
else
if [ "$character" = "2" ]; then
echo "You entered two."
else
if [ "$character" = "3" ]; then
echo "You entered three."
else
echo "You did not enter a number"
echo "between 1 and 3."
fi
fi
fi
A more fancy menu:
selection=
until [ "$selection" = "0" ]; do
echo ""
echo "PROGRAM MENU"
echo "1 - display free disk space"
echo "2 - display free memory"
echo ""
echo "0 - exit program"
echo ""
echo -n "Enter selection: "
read selection
echo ""
case $selection in
1 ) df ;;
2 ) free ;;
0 ) exit ;;
* ) echo "Please enter 1, 2, or 0"
esac
done


Recent Comments