Sei sulla pagina 1di 36

The Unix system is a multi-user, multi tasking operating system which means that it allows a single or multiprocessor computer

to simultaneously execute several programs by one or several users. It has one or several command interpreters (shell) as well as a great number of commands and many utilities (assembler, compilers for many languages, text processing, email, etc.). Furthermore, it is highly portable, which means that it is possible to implement a Unix system on almost all hardware platforms.

The shell is a command programming language that

provides an interface to the UNIX operating system. Its features include control-flow primitives, parameter passing, variables and string substitution. The shell can modify the environment in which commands run. Input and output can be redirected to files, and processes that communicate through `pipes' can be invoked. Commands can be read either from the terminal or from a file, which allows command procedures to be stored for later use

KSH CSH TCSH Z BASH Sh

ls list cd - change directory grep wc word count who who is logged in mkdir make directory chmod change mode cut -- cut command [cut -c1-6 <file> ,cut c 4,8 <file> , cut -d: -f3 <file>]

ps process running pwd present working directory date current system date cal calendar echo print on standard output rm remove cp copy mv move whereis finds program find search man manual zip Compress and zips file [gzip,bzip etc.,]

u - File owner. g - Group and extended ACL entries pertaining to the file's group. o - All others. a - User, group, and all others. The flag has the same effect as specifying the ugo flags together..

ls -l >file ls -l >>file wc <file (Counts the number of lines, words, bytes, or characters in a file) wc -l <file

ls -l | wc ls -l >file; wc <file ls | grep old who | sort ls | grep old | wc -l

$! -The process number of the last process run in the background (in decimal). $MAIL $HOME $PATH $?

String operators s1 = s2 s1 != s2 s1 < s2 s1 > s2 Arithmetic operators -lt (<) -gt (>) -le (<=) -ge (>=) -eq (==) -ne (!=)

Test if a string is of length 0 [ -z $STRING ] Test if length of string is not zero: [ ! -z $STRING ] [ -n $STRING ] Test if strings are equal. [ $STRING1 == $STRING2 ] Remember to use quotes if the string has spaces or escape characters (newlines). [ "$STRING1" == "$STRING2" ] The 'not equal; operator is != [ $STRING1 != $STRING2 ]

Which command can be used to see all the files in a directory, time and permission with ascending order?. Give the command. A file trainee needs to be renamed as employee. Which command do you use?. Write the command A script you created could not run. What are the commands you use to check and change permissions?. Find out how you view a file with .gz without unzipping or decompressing.

Command and insert mode. Navigation Correction Saving Searching Replacing view command

j or <Return> [or down-arrow] move cursor down one line k [or up-arrow] move cursor up one line h or <Backspace> [or left-arrow] move cursor left one character l or <Space> [or right-arrow] move cursor right one character 0 (zero) move cursor to start of current line (the one with the cursor) $ move cursor to end of current line w move cursor to beginning of next word

b move cursor back to beginning of preceding word


:0<Return> or 1G move cursor to first line in file :n<Return> or nG move cursor to line n :$<Return> or G move cursor to last line in file i insert text before cursor, until <Esc> hit I insert text at beginning of current line, until <Esc> hit a append text after cursor, until <Esc> hit A append text to end of current line, until <Esc> hit

#!/usr/bin/bash echo "Then the master said, \"Remember your training. Use it whenever needed\""; exit 0;

bash$ 2 bash$ 6 bash$ 2 bash$ 0 bash$ 1 bash$ 0 bash$

expr 1 + 1 expr 3 \* 2 expr 6 / 3

expr 6 % 3
expr 3 / 2

expr 3 / 6
expr 6 \* 3.5

bash$ echo $((2 + 2)) 4 bash$ echo $[2 + 2] 4

Floating Point Arithmetic in Bash Using floating point in bash scripts requires an external calculator like GNU bc. Escaping is not needed for quoted asterisks. bash$ echo "3.8 + .4" | bc 4.2 bash$ echo '6 * 1.5' | bc 9.0

If all are integers, the bc option must be defined if you expect a floating point result. bash$ echo '2 / 5' | bc 0 bash$ echo 'scale=2; 2 / 5' | bc .40 bash$ echo 'scale=2; 2 / 5' | bc .40 bash$ bc <<< 'scale=2; 2 / 5' .40

#!/usr/bin/ksh i=0; while [ $i -lt 5 ]; do echo $i; i=`expr $i + 1`; done

#!/usr/bin/ksh i=0 while [ $i -lt 5 ] do i=`expr $i + 1` echo $i done

j=$i until [ ! $j -lt 10 ] do j=`expr $j + 1` echo $j done

#!/bin/sh For a in 0 1 2 3 4 do echo $a done

for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done

PS3="SELECT ONE OPTION:" select SIGN in date dspace host exit do case $SIGN in date) date;; dspace) df -k . ;; host) uname -a ;; exit) exit 0 ;; *) echo "ERROR: Invalid selection" ;; esac done

#!/bin/bash function quit { exit }

function hello { echo Hello! } hello quit

Pass variables into the function by space delimited fields and accept parameters using $1, $2 and so on. my_function() { echo "$1" } my_function hello this is a test hello To expand all variable parameters inside the function, use $@ log() { echo "[$(date)]: $@" } log this is a test [Mon Jan 22 23:21:26 2010this is a test

Functions return an exit status to their caller: test() { return 1 } test || echo "Returned: $?" Returned: 1

#!/bin/bash OPTIONS="Hello Quit" select opt in $OPTIONS; do if [ "$opt" = "Quit" ]; then echo done exit elif [ "$opt" = "Hello" ]; then echo Hello World else clear echo bad option fi done

#!/usr/bin/bash list1=(sam ram); echo ${list1[0]};

#!/bin/bash echo Please, enter your name read NAME echo "Hi $NAME!"

$RANDOM to produce a random integer between 0 and 32767. bash$ echo $RANDOM 333 bash$ echo $RANDOM 2321 bash$ echo $RANDOM 23534 For a single digit Integer: let R=$RANDOM%10; echo $R For a number between 0 and 99 let R=$RANDOM%100; echo $R For a number between 1 and 100 let R=$RANDOM%100+1; echo $R

#!/bin/bash a=`cat $1 | wc -l` echo The file $1 contains $a lines.

sed s/day/night/ <old >new awk '{ print $2 }' inven print column two of inven awk '/good/ { print $3 }' inventory (searches for good string and prints 3 rd col

stderr to file grep hda6 * . 2> stderr.txt stdout to stderr grep -r hda6 * . 1>&2 stderr.txt stderr to stdout grep -r hda6 * . 2>&1 stderr.txt stderr and stdout to file grep -r hda6 * . &> stderr_and_stdout.txt

Find the number of lines in all the files in a directory. Constraint: You cant save code in a file and execute. Hint: Use a loop. Read from a file which lists file name and find is any word matches a particular string which you are searching. Use either loop or file coding.

Write the command to get all the directories in any one PATH

FILE AND DIRECTORY MANAGEMENT COMMANDS 1 -- Display the contents of a file 2 -- Remove a file 3 -- Copy a file get filename that is to be copied from user 4 -- List files in dir [Get dir from user] 5 -- Size of a file 6 -- System date 7. Number of files in current dir 8. Names of all the files in a dir 9. Last modified file 10. Calculator 11. -- Quit -Enter your choice:

Potrebbero piacerti anche