Shell Scripting
Shell Scripting
For this Purpose To insert new text To save file To save file with file name (save as) To quit the vi editor To quit without saving To save and quit vi editor To search for specified word in forward direction To continue with search To search for specified word in backward direction To copy the line where cursor is located To paste the text just deleted or copied at the cursor To delete entire line where cursor is located To delete word from cursor position Use this vi Command Syntax esc + i ( You have to press 'escape' key then 'i') esc + : + w (Press 'escape' key then 'colon' and finally 'w') esc + : + w "filename" esc + : + q esc + : + q! esc + : + wq esc + /word (Press 'escape' key, type /word-tofind, for e.g. to find word 'shri', type as /shri) n esc + ?word (Press 'escape' key, type word-tofind) esc + yy esc + p esc + dd esc + dw esc + :$s/word-to-find/word-to-replace/g For. e.g. :$s/mumbai/pune/g Here word "mumbai" is replace with "pune"
To Find all occurrence of given word and Replace then globally without confirmation
To Find all occurrence of given word and Replace then globally with confirmation To run shell command like ls, cp or date etc within vi
Command Explanation All occurrence g [^] This means not /[^ Empty line, Combination of ^ /^$ and $.
There are certain named classes of characters which are predefined. They are as follows:
Predefined classes of characters [:alnum:] [:alpha:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:]
Meaning Letters and Digits (A to Z or a to z or 0 to 9) Letters A to Z or a to z Delete character or ordinary control character (0x7F or 0x00 to 0x1F) Digit (0 to 9) Printing character, like print, except that a space character is excluded Lowercase letter (a to z) Printing character (0x20 to 0x7E) Punctuation character (ctrl or space) Space, tab, carriage return, new line, vertical tab, or form feed (0x09 to 0x0D, 0x20) Uppercase letter (A to Z) Hexadecimal digit (0 to 9, A to F, a to f)
\033 [ 34 m
Character or letter Use in CSI h Set the ANSI mode l Clears the ANSI mode Useful to show characters in different colors or effects such m as BOLD and Blink, see below for parameter taken by m. Turns keyboard num lock, caps lock, scroll lock LED on or off, q see below. Stores the current cursor x,y s position (col , row position) and attributes Restores cursor position and u attributes
echo -e "\033[2q"
1 2
$ echo -e "I am \033[1m BOLD \033[0m Person" I am BOLD Person Prints BOLD word in bold intensity and next ANSI Sequence remove bold effect (\033[0m) $ echo -e "\033[1m BOLD \033[2m DIM
5 7
11
25 27
30 - 37
40 - 47
Blink Effect Reverse video effect i.e. Black foreground and white background in default color scheme Shows special control character as graphics character. For e.g. Before issuing this command press alt key (hold down it) from numeric key pad press 178 and leave both key; nothing will be printed. Now give --> command shown in example and try the above, it works. (Hey you must know extended ASCII Character for this!!!) Removes/disables blink effect Removes/disables reverse effect Set foreground color 31 - RED 32 - Green xx - Try to find yourself this left as exercise for you:-) Set background color xx - Try to find yourself this left as exercise for you:-)
\033[0m" $ echo -e "\033[5m Flash! \033[0m" $ echo -e "\033[7m Linux OS! Best OS!! \033[0m"
$ press alt + 178 $ echo -e "\033[11m" $ press alt + 178 $ echo -e "\033[0m" $ press alt + 178
Meaning Number of command line arguments. Useful to test no. of command line args in shell script. All arguments to shell Same as above Option supplied to shell PID of shell PID of last started background process (started with &)
To find all available shells in your system type following command: $ cat /etc/shells
running process To stop all process except your kill 0 shell For background processing (With &, use to put particular command linux-command & and program in background) To display the owner of the processes along with the ps aux processes To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command
$ kill 0 $ ls / -R | wc -l &
$ ps aux For e.g. you want to see whether Apache web server process is running or not then give command $ ps ax | grep httpd $ top
Note that to exit from top command press q.
To see currently running processes and other information like memory top and CPU usage with real time See the output of top command. updates. To display a tree of processes pstree
$ pstree
* To run some of this command you need to be root or equivalnt user. NOTE that you can only kill process which are created by yourself. A Administrator can almost kill 95-98% process. But some process cannot be killed, such as VDU Process.
-eq -ne
5 == 6 5 != 6
to is less than is less than or equal to is greater than is greater than or equal to
Logical Operators Logical operators are used to combine two or more condition at a time
Operator ! expression expression1 -a expression2 expression1 -o expression2 Meaning Logical NOT Logical AND Logical OR
Use dialog utility to Display dialog boxes from shell scripts. Syntax:
dialog --title {title} --backtitle {backtitle} {Box options} where Box options can be any one of following --yesno {text} {height} {width} --msgbox {text} {height} {width} --infobox {text} {height} {width} --inputbox {text} {height} {width} [{init}] --textbox {file} {height} {width} --menu {text} {height} {width} {menu} {height} {tag1} item1}...
-n