Professional Documents
Culture Documents
-----------------
$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)
$ 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)
$ a=5
let - used for Simple calculations $ let a=a+1
$echo $a
$ a=5
(( )) - used for Simple calculations $ (( a+1 ))
$echo $a
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 $.
#!/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
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 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
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
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"