1.presentation Command Line and Shell Programming
1.presentation Command Line and Shell Programming
http://foss.coep.org.in/fossmeet
Abhijit A. M.
abhijit.comp@coep.ac.in
COEP’s Free Software Users Group
(COFSUG)
Why GNU/Linux ?
Why GNU/Linux ?
ls -l /home/student/Desktop
ls -l ./Desktop navigation using
ls -a a graphical file
\ls -F browser
cd
cd /tmp/
cd
cd /home/student/Desktop
notation: ~
cd ~
cd ~/Desktop
ls ~/Desktop
The Shell
Shell = Cover
Covers the
Operating System's
“System Calls” for
the Applications
Talks with Users
and Applications
and does some talk
with OS
GUI is a Shell !
Let's Understand fork() and
exec()
#include <unistd.h> #include <unistd.h>
int main() { int main() {
fork(); printf("hi\n");
printf("hi\n"); execl("/bin/ls",
"ls", NULL);
return 0;
printf("bye\n");
}
return 0;
}
A simple shell
#include <stdio.h>
#include <unistd.h>
int main() {
char string[128];
int pid;
while(1) {
printf("prompt>");
scanf("%s", string);
pid = fork();
if(pid == 0) {
execl(string, string, NULL);
} else {
wait(0);
}
}
}
File Permissions on Linux
Two types of users
root and non-root
Users can grouped into 'groups'
3 sets of 3 permission
Octal notation
Read = 4, Write = 2, Execute = 1
644 means
Read-Write for owner, Read for Group, Read for others
chmod command uses these notations
File Permissions on Linux
http//free-
electrons.com
Man Pages
Manpage 4 Device drivers and
$ man ls network protocols
$ man 2 mkdir
/dev/tty
$ man man
5 Standard file formats
$ man -k mkdir
/etc/hosts
Manpage sections
6 Games and demos
/usr/games/fortune
1 User-level
cmds and apps
7 Misc. files and docs
/bin/mkdir
man 7 locale
2 System calls
8 System admin. Cmds
int mkdir(const char
/sbin/reboot
*, …);
3 Library calls
int printf(const char
*, …);
http://www.cs.ucr.edu/~weesan/cs183/
GNU / Linux filesystem
structure
Not imposed by the system. Can vary from one
system to the other, even between two GNU/Linux
installations!
/ Root directory
/bin/ Basic, essential system commands
/boot/ Kernel images, initrd and
configuration files
/dev/ Files representing devices
/dev/hda: first IDE hard disk
/etc/ System and application configuration
files
/home/ User directories
/lib/ Basic system shared libraries
http//free-
electrons.com
GNU / Linux filesystem
structure
/lost+found Corrupt files the system tried to
recover
/media Mount points for removable media:
/media/usbdisk, /media/cdrom
/mnt/ Mount points for temporarily
mounted filesystems
/opt/ Specific tools installed by the
sysadmin
/usr/local/ often used instead
/proc/ Access to system information
/proc/cpuinfo, /proc/version ...
/root/ root user home directory
/sbin/ Administrator-only commands
/sys/ System and device controls
(cpu frequency, device power, etc.)
http//free-
electrons.com
GNU / Linux filesystem
structure
/tmp/ Temporary files
/usr/ Regular user tools (not essential to the
system)
/usr/bin/, /usr/lib/, /usr/sbin...
/usr/local/ Specific software installed by the
sysadmin
(often preferred to /opt/)
/var/ Data used by the system or system
servers
/var/log/, /var/spool/mail (incoming
mail), /var/spool/lpd (print jobs)...
http//free-
electrons.com
Files: cut, copy, paste,
remove,
cat <filenames>
mv <source> <target>
cat /etc/passwd
mv a.c b.c
cat fork.c
mv a.c /tmp/
cat <filename1>
<filename2>
mv a.c /tmp/b.c
cp <source> <target>
rm <filename>
cp a.c b.c
rm a.c
cp a.c /tmp/
rm a.c b.c c.c
cp a.c /tmp/b.c
rm -r /tmp/a
cp -r ./folder1 /tmp/
mkdir
cp -r ./folder1 /tmp/folder2
mkdir /tmp/a /tmp/b
rmdir
rmdir /tmp/a /tmp/b
Useful Commands
echo
grep
echo hi
grep bash /etc/passwd
echo hi there
grep -i display
echo “hi there” /etc/passwd
j=5; echo $j
egrep -i 'a|b'
/etc/passwd
sort
less <filename>
sort
sort < /etc/passwd
head <filename>
firefox
head -5 <filename>
libreoffice
tail -10 <filename>
Useful Commands
alias
alias ll='ls -l'
strings
tar
strings a.out
tar cvf folder.tar
folder
adduser
sudo adduser test
gzip
gzip a.c
su
su administrator
touch
touch xy.txt
touch a.c
Useful Commands
df
df -h
du
du -hs .
bc
time
date
diff
wc
Network Related Commands
ifconfig
ping
ssh
w
scp
last
telnet
whoami
Unix job control
• Start a background process:
– gedit a.c &
– gedit
hit ctrl-z
bg
• Where did it go?
– jobs
– ps
• Terminate the job: kill it
– kill %jobid
– kill pid
• Bring it back into the foreground
– fg %1
Shell Wildcards
? (question mark)
[]
Any ene character
Matches a range
ls a?c
ls ??c
ls a[1-3].c
*
{}
any number of
ls pic[1-3].
characters
{txt,jpg}
ls *
ls d*
echo *
~/.bashrc file
~/.bashrc
Shell script read each time a bash
shell is started
You can use this file to define
Your default environment variables (PATH,
EDITOR...).
Your aliases.
Your prompt (see the bash manual for details).
A greeting message.
http//free-
electrons.com
Special devices (1)
Device files with a special behavior or contents
/dev/null
The data sink! Discards all data written to this
file.
Useful to get rid of unwanted output, typically
log information:
mplayer black_adder_4th.avi &> /dev/null
/dev/zero
Reads from this file always return \0
characters
Useful to create a file filled with zeros:
dd if=/dev/zero of=disk.img bs=1k count=2048
http//free-
electrons.com
Special devices (2)
/dev/random
Returns random bytes when read. Mainly used
by cryptographic programs. Uses interrupts
from some device drivers as sources of true
randomness (“entropy”).
Reads can be blocked until enough entropy is
gathered.
/dev/urandom
For programs for which pseudo random
numbers are fine.
Always generates random bytes, even if not
enough entropy is available (in which case it is
possible, though still difficult, to predict future
byte sequences from past ones).
See man random for details.
http//free-
electrons.com
Special devices (3)
/dev/full
Mimics a full device.
Useful to check that your application properly
handles
this kind of situation.
See man full for details.
http//free-
electrons.com
Files names and inodes
Filesystem
http//free-
electrons.com
Shell Programming
Shell Programming
is “programming”
Any programming: Use existing tool
to create new utilities
Shell Programming: Use shell
facilities to create better utilities
Know “commands” --> More tools
Know how to combine them
Shell Variables
Shell supports variables
Try:
j=5; echo $j
No space in j=5
Try
set
Shows all set variables
Try
a=10; b=20; echo $a$b
What did you learn?
All variables are strings
Shell's predefined variables
USER
HOSTNAME
Name of current user
Name of the computer
OLDPWD
HOME
Previous working directory
Home directory of
current $USER
PATH
List of locations to search
PS1 for a command's
The prompt executable file
LINES
$?
Return value of previou
No. of lines of the command
screen
Redirection
cmd > filename
Redirects the output to a file
Try:
ls > /tmp/xyz
cat /tmp/xyz
echo hi > /tmp/abc
cat /tmp/abc
cmd < filename
Reads the input from a file instead of keyboard
Think of a command now!
Pipes
Try
last | less
grep bash /etc/passwd | head – 1
grep bash /etc/passwd | head – 2 | tail -1
Connect the output of LHS command as input of
RHS command
Concept of filters – programs which read input
only from stdin (keyboard, e.g. scanf, getchar),
and write output to stdout (e.g. printf, putchar)
Programs can be connected using pipes if they
are filters
Most Unix/Linux commands are filters !
The test command
test Shortcut
test 10 -eq 10 notation for
test “10” == “10” calling test
test 10 -eq 9
[]
test 10 -gt 9
test “10” >= “9” [ 10 -eq 10 ]
test -f /etc/passwd Note the space
test -d ~/desktop after '[' and
... before ']'
The expr command and
backticks
expr
backticks ``
expr 1 + 2
j=`ls`; echo $j
a=2; expr $a + 2
j=`expr 1 + 2`;
a=2; b=3; expr $a + echo $j
$b
a=2;b=3; expr $a \*
$b
a=2;b=3; expr $a | $b
Used for mathematical
calculations
if then else
if [ $a -lt $b ] if [ $a -lt
then $b ];then
echo $a; else
echo $a echo $b; fi
else 0 TRUE
echo $b Nonzero FALSE
fi
while do done
while [ $a -lt $b ] while [ $a -lt $b ]; do
do echo $a; a=`expr
echo $a $a + 1`; done
a=`expr $a + 1`
done
while [ $a -lt $b ]
do
echo $a
a=$(($a + 1))
done
for x in ... do done
for i in {1..10} for i in *; do echo
do $i;
echo $i done
done
for i in *
do
echo $i
done
read space
case $space in
[1-6]*)
case ... esac
Message="one to 6"
;; Syntax
[7-8]*) ;;
Message="7 or 8"
) after option
;;
* for default
9[1-8])
Message="9 with a number" esac
;;
*)
Message="Default"
;;
esac
echo $Message
Try these things
Print 3rd line from /etc/passwd, which
contains the word bash
Print numbers from 1 to 1000
Create files named like this: file1, file2,
file3, ... filen where n is read from user
Read i%5th file from /etc/passwd and store it
in filei
Find all files ending in .c or .h and create
a .tar.gz file of these files
The Golden Mantra