0% found this document useful (0 votes)
68 views

Shell Scripting

Common vi editor command list for this Purpose To insert new text to save file to quit the vi editor to quit without saving to search for specified word in forward direction to continue with search to copy the 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')

Uploaded by

Ankush Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Shell Scripting

Common vi editor command list for this Purpose To insert new text to save file to quit the vi editor to quit without saving to search for specified word in forward direction to continue with search to copy the 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')

Uploaded by

Ankush Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Common vi editor command list

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

esc + :$s/word-to-find/word-to-replace/cg esc + :!shell-command For e.g. :!pwd

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

Escape character Start of CSI 34 is parameter m is letter (specifies action)

Following table show important list of such escape-code/action letter or character

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

Examples echo -e "\033[h" echo -e "\033[l" echo -e "\033[35m Hello World"

echo -e "\033[2q"

echo -e "\033[7s" echo -e "\033[8u"

m understand following parameters


Parameter 0 Meaning Sets default color scheme (White foreground and Black background), normal intensity, no blinking etc. Set BOLD intensity Set dim intensity Example

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

$ echo -e "\033[31m I am in Red"

$ echo -e "\033[44m Wow!!!"

q understand following parameters


Parameters 0 1 2 3 Meaning Turns off all LEDs on Keyboard Scroll lock LED on and others off Num lock LED on and others off Caps lock LED on and others off

Shell Built in Variables

Shell Built in Variables $# $* $@ $$$ $!

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

To find your current shell type following command $ echo $SHELL

Linux Command Related with Process


Following tables most commonly used command(s) with process:
For this purpose To see currently running process To stop any process by PID i.e. to kill process To stop processes by name i.e. to kill process To get information about all Use this Command ps kill {PID} $ ps $ kill 1012 $ killall httpd $ ps -ag Examples*

killall {Process-name} ps -ag

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.

ps ax | grep process-U-want-to see

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.

For Mathematics, use following operator in Shell Script


Mathematical Operator in Shell Script Meaning Normal Arithmetical/ Mathematical Statements But in Shell For test For [ expr ] statement with statement with if if command command if test 5 -eq 6 if [ 5 -eq 6 ] if test 5 -ne 6 if [ 5 -ne 6 ]

-eq -ne

is equal to is not equal

5 == 6 5 != 6

-lt -le -gt -ge

to is less than is less than or equal to is greater than is greater than or equal to

5<6 5 <= 6 5>6 5 >= 6

if test 5 -lt 6 if test 5 -le 6 if test 5 -gt 6 if test 5 -ge 6

if [ 5 -lt 6 ] if [ 5 -le 6 ] if [ 5 -gt 6 ] if [ 5 -ge 6 ]

NOTE: == is equal, != is not equal.

For string Comparisons use


Operator string1 = string2 string1 != string2 string1 -n string1 -z string1 Meaning string1 is equal to string2 string1 is NOT equal to string2 string1 is NOT NULL or not defined string1 is NOT NULL and does exist string1 is NULL and does exist

Shell also test for file and directory types


Test -s file -f file -d dir -w file -r file -x file Meaning Non empty file Is File exist or normal file and not a directory Is Directory exist and not a file Is writeable file Is read-only file Is file is executable

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

Standard File stdin stdout stderr

File Descriptors number 0 1 2

Use as Standard input as Standard output as Standard error

Example Keyboard Screen Screen

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}...

For input box's exit status refer the following table:


Exit Status for Input Meaning box 0 Command is successful 1 Cancel button is pressed by user 255 Escape key is pressed by user

Syntax: trap {commands} {signal number list}


Signal Number 0 1 2 3 9 When occurs shell exit hangup interrupt (CTRL+C) quit kill (cannot be caught)

common awk metacharacters:


Metacharacter . (Dot) * ^ $ \ [] {} + ? | Meaning Match any character Match zero or more character Match beginning of line Match end of line Escape character following List Match range of instance Match one more preceding Match zero or one preceding Separate choices to match

Following table shows list of such built in awk variables.


awk Variable FILENAME RS OFS ORS NF NR OFMT FS Meaning Name of current input file Input record separator character (Default is new line) Output field separator string (Blank is default) Output record separator string (Default is new line) Number of input record Number of fields in input record Output format of number Field separator character (Blank & tab is default)

Following table shows such common format specification code:


Format Specification Code %c Character } { %d Decimal number such as 10,-5 etc } { %x Hexadecimal number such as 0xA, 0xffff etc } { %s String such as "vivek", "Good buy" } str1 = "Welcome to Linux!" printf "%s", str1 printf "%s", "Can print ?" n = 10 printf "%x",n n = 10 printf "%d",n Meaning { isminor = "y" printf "%c" , isminor Example

You can use following assignment operator:


Assignment operator += -= *= %= Use for Assign the result of addition Assign the result of subtraction Assign the result of multiplication Example Equivalent to a = a + 10 a=a+c a = a - 10 a=a-c a = a * 10 a=a*c a = a % 10 a=a%c

a += 10 d += c a -= 10 d -= c a *= 10 d *= c a %= 10 Assign the result of modulo d %= c

Option can be:


Option -e -f Meaning Read the different sed command from command line. Read the sed command from sed script file. Suppress the output of sed command. When -n is used you must use p command of print flag. Example $ sed -e 'sed-commands' data-file-name $ sed -e 's/Linux/UNIX(system v)/' demofile1 $sed -f sed-script-file data-file-name $ sed -f chgdb.sed friends.tdb $ sed -n '/^\*..$/p' demofile2

-n

You might also like