Você está na página 1de 6

VI Commands

-----------------

i – Goes into the input mode at cursor position.


pressing Esc - Changes into command mode.
a – Goes into the input mode at cursor position + 1
A – Start insertion from end of line
c – Change mode. Start replacing from cursor
position.
dd – Deletes the current line
:w – Perform a save operations
:wq – Save and Exit
:q! – Discard the changes and exit.
:w <file> – Save into a different file name.
dw – Delete word
d$ – Delete till end of line
/<string> – Search for <string>
n – Search for the next occurrence (find next)
N – Search backwards (find previous)
:e <file> – Edit the file name <file>
dG – Deletes till end of file
u – Undo the last command

Definition & Syntax : Example :

Popular Unix commands : $ pwd


pwd – Show current working directory $ mkdir <dir>
ls – List directory contents $ cd <dir>
mkdir – Create Directory $ vi <fiile>
cd – Change directory $ ls
cp – Copy files $ cp <file> <another file>
mv – Move/Rename files $ mv <file> <another file>
rm – Remove files $ rm <file>
$cd ..
$ls

cat – Show file contents $ cat <file>


pg - Show file contents page by page $ cat <file> | pg
more – Show file contents page by page $ cat <file> | more
date – Displays system date $ date
head – Shows the initial part of the files $ head <file>
tail – Shows the trailing part of the file $ tail <file>
diff – Shows the difference in file contents $ diff <file> <anotherfile>
touch – Create file with zero bytes or mark files modified $ touch <file>
clear – Clear screen $ clear

$ls -l hosts
-rw-r--r-- 1 natasha staff
ls -l : shows a long listing of files and directories. The first
983 Apr 12 01:08 hosts
character of first column can be 'd' meaning it's a directory or '-'
meaning it's aregular file.
characters 2-4 : Owner Access(r= read w=write x= execute)
characters 5-7 : Group Access(r= read w=write x= execute)
character 8-10 : Other Access(r= read w=write x= execute)

$ chmod u+rwx,g-rxw,o-rwx
hosts
$ ls -l hosts
chmod – Change file permissions for u = user(owner), g = -rwx------ 1 natasha staff
group, o = others and a = all using + as grant, - as revoke and 983 Apr 12 01:08 hosts
= as setting different access permissions. $chmod u=rw-,g=r--,o=---
<file>
There is a numeric equivalent for the access permissions r= 4 $chmod a+rw <file>
w=2 x= 1. If u want to give r+w permission then 4+2 ie. 6 has
to be put at 1st place for user, 2nd place for group and 3rd $ chmod 766 <file>
place for others. $chmod +11 <file> (This add
execution mode for group and
others)

passwd – Command to change the user password $ passwd


uid – Shows the current user & group details $ uid
ps – Shows the current processes running $ ps
command1 | command2 - pipe the output of command1 to the $ps | grep oracle*
input of command2 $who | wc -l

$ ls -l > list1
$ who > list2
command > file - redirect standard output to a file $ cat list1 list2 > biglist
command >> file - append standard output to a file $ cat list3 >> biglist
command < file - redirect standard input from a file $ sort < biglist
$ sort < biglist > slist (The
sorted biglist is put into slist)

rmdir – Remove directories $ rmdir <dir>


echo – Prints the argument values to screen $ x=5
$ echo $x
$ sort <file>
sort – Shows the file contents after sorting $ find . -name <file> -print
find – Command to search for file (s) in a directory structure $ expr 5+3-1
expr – Performing Arithmetic. $ read p
read – Reading input value into a variable $ echo $p
cut -f1 -d: - cuts out the first field from each line using ':' as $ cat <file> | cut -f1 -d:
the delimiter $ ls -l > sname
tr - Translates one case to another $ tr "[a-z]" "[A-Z]" < sname >
cap_names

$ a=5
let - used for Simple calculations $ let a=a+1
$echo $a

$ a=5
(( )) - used for Simple calculations $ (( a+1 ))
$echo $a

$ cat <file> | grep ashok*


$ grep -c 'pattern' file

?(pattern) matches zero or


one times the pattern.
*(pattern) matches any time
the pattern.
grep – Command to search a file for pattern +(pattern) matches one or
more time the pattern.
@(pattern) matches one time
the pattern.
!(pattern) matches string
without the pattern.

eg. if [[ $var = fo@(?


4*67).c ]];

Definition & Syntax : Example :

Unix Scripts :
Variables are sequences of letters, digits or underscores $PATH=/usr/ucb:/usr/bin:/bin;
beginning with a letter or underscore.Assign a value to a
variable by variable=value.

To get the contents of a variable you must prepend the $ echo $PATH
name with a $.

Command line arguments to shell scripts are positional $ vi demo


#!/bin/sh
variables: echo "Total number of command line argument are $#"
$0 - Script file name echo "$0 is script name"
$1 - 1st argument
$2 - 2nd argument echo "$1 is first argument"
echo "$2 is second argument"
$# - The number of arguments echo "All of them are :- $* or $@"
$*, $@ - All arguments separated by Blank $ chmod 755 demo
$? - Exit status of last command. $demo Hello World
$$ - Process id of current program.

#!/bin/bash
Single and Double Quotes : echo -n '$USER='
- Variable names are exapnded within double quotes, but echo "$USER"
echo "\$USER=$USER" # Backspace is escape character,
not in single quotes so here $ will not be interpreted

if [ "`echo -n`" = "-n" ]; then


n=""
Back Quotes c="\c"
else
Back quotes mean run the command and substitute the n="-n"
output. c=""
fi
$ TODAY=`(set \`date\`; echo $1)`

A command ends either at the end of the line or whith a $print -n "Name: "; read name; print ""
$grep filename | sort -u | awk '{print $4}' | \
";". So one can put several commands onto one line. uniq -c >> /longpath/file

One can continue commands over more than one line with
a "\" immediately followed by a newline sign which is
made by the return key.

The script starts at the first line and ends either when it
encounters an "exit" or the last line.
# Usage of array of variable
All "#" lines are ignored. arrname[1]=4 #To fill in
print ${arraname[1]} #To print out
${arrname[*]} #Get all elements
Set and use a variable array like .. ${#arrname[*]} #Get the number of elements

test expression OR [ expression ] $ cat > ispostive


#!/bin/sh
Test Nummeric values : if test $1 -gt 0
-eq is equal to then
-ne is not equal to echo "$1 number is positive"
fi
-lt is less than
-le is less than or equal to if [ 5 -eq 6 ]
-gt is greater than
-ge is greater than or equal to

Test Strings :
if test string1 = string2
string1 is equal to string2 if test string1 != string2
string1 is NOT equal to string2 if test string1
string1 is NOT NULL or not defined

string1 is NOT NULL and does exist if test -n string1


if [ -z string1 ]
string1 is NULL and does exist

Test file and directory types :


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

command1 && comamnd2|| command3


$ rm myf && echo "File is removed successfully" || echo
if command1 is executed successfully then shell will run "File is not removed"
command2 and if command1 is not successful then
command3 is executed.

Conditional Execution if [ $# -ne 3 ]; then


Test exit status of command and branch echo Usage: $0 19 Oct 91
if command exit 127
fi
then
command if [[ $name = "John" ]];then
[ else print "Your welcome, ${name}."
elif [[ $name = "Hanna" ]];then
command ] print "Hello, ${name}, who are you?"
fi else
print "Good bye, ${name}!"
fi
if .. then .. elif .. then .. else .. fi

For loop iteration


for i in `cat $LOGS`
Substitute values for variable and perform task: do
for variable in word ... mv $i $i.$TODAY
do cp /dev/null $i
chmod 664 $i
command done
done

$ cat > car


if [ -z $1 ]
then
rental="*** Unknown vehicle ***"
elif [ -n $1 ]
Case then
Switch to statements depending on pattern match # otherwise make first arg as rental
rental=$1
case word in fi
[ pattern [ | pattern ... ] )
command ;; ] ... case $rental in
esac "car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not gat a $rental for you";;
esac

# for each argument mentioned, purge that directory

while [ $# -ge 1 ]; do
_purge $1
shift
While/Until Iteration done
#The "shift" command can be used to shift command line
Repeat task while command returns good exit status. arguments to the left, ie $2 shifts into $1, $3 shifts into
{while | until} command $2, etc.
do
{ while read myline;do
command echo $myline
done done } < filename

last | sort | {
while read myline;do
echo $myline
done }
From eof to eof all is feeded into the mentioned command.
wc -l<<EOF
command <<EOF for i in `cat $LOGS`
do
input1 echo $i
input2 done
input3 EOF
EOF

In Linux and in C,your keyboard, screen etc are all treated


as files. Following are name of such files ..
$ rm bad_file_name 2>myerr
stdin 0 as Standard input - Keyboard $cat myerr
stdout 1 as Standard output - Screen
stderr 2 as Standard error - Screen

if [ $# -ne 2 ]
then
Redirecting stdout: echo "Error : Number are not supplied" 1>&2
echo "Usage : $0 number1 number2" 1>&2
from>&destination exit 1
1>&2 means the stdout(1) will be redirected to stderr(2) fi
ans=`expr $1 + $2`
echo "Sum is $ans"

Você também pode gostar