UNIX Linux Basics Final

download UNIX Linux Basics Final

of 72

Transcript of UNIX Linux Basics Final

  • 7/30/2019 UNIX Linux Basics Final

    1/72

    1

    UNIX/Linux Overview

  • 7/30/2019 UNIX Linux Basics Final

    2/72

    2

    Object ives What is Unix?

    Why do I need to know Unix?

    Unix history.

    Architecture of Unix/Linux.

    Important files and commands.

    How is Unix/Linux booted?

  • 7/30/2019 UNIX Linux Basics Final

    3/72

    3

    What is Unix?

    What is an operating system?

    The low-level (first layer loaded) softwarewhich handles the interface to system hardware(input/output devices, memory, file system, etc),

    schedules tasks, and provides common coreservices such as a basic user interface.

    Colloquially, all of the software that comeswith a system before applications are installed.

  • 7/30/2019 UNIX Linux Basics Final

    4/72

    4

    What is Unix?A por tab le, mu lt i-task ing and

    mult i -user operat ing system

    Portable: runs on many different hardwarearchitectures (Intel x86 and IA-64, Alpha,

    MIPS, HP PA-RISC, PowerPC, IBM S/390,SPARC, Motorola 680x0, etc.).

    Preemptive multi-tasking: several programs

    can run at the same time (time slices,interrupts, and task switching).

    Multi-user: many users can share thecomputer system at the same time.

  • 7/30/2019 UNIX Linux Basics Final

    5/72

    5

    What is Unix?Other Featu res

    Uses a simple, uniform file model which includesdevices and access to other services in aflexible, hierarchical file system.

    Written in a high-level language (C) making iteasy to read, understand, change and port.

    The command prompt is a simple user process,the Unix shell, which is also a convenient jobprogramming language.

    Includes support forregular expressions whichare convenient for complex searching.

  • 7/30/2019 UNIX Linux Basics Final

    6/72

    Is L INUX really UNIX?

    Well , yes and no :

    Yes, because it has essent ial ly the same look and

    feel l ike any UNIX operating Sys tem.

    Yes, because it offers the abi l i ty to run nearly any

    prog ram that runs on UNIX systems (through API

    convention s such as POSIX, etc..).

    No because the heart of the system (kernel) has a

    lot of new features that go beyond the classical

    design phi losop hy of UNIX kernels.

  • 7/30/2019 UNIX Linux Basics Final

    7/727

    Unix Histo ry

    1964 joint project between AT&T Bell Labs,GE, and MIT to develop a new OS.

    Goal : develop an OS that could providecomputational power, data storage and theability to share data among multiple users.

    Result: Multiplexed Information & ComputerService - MULTICS.

  • 7/30/2019 UNIX Linux Basics Final

    8/728

    Unix Histo ry

    1969 Bell Labs withdraws from group.

    Two Bell Lab scientists, Ken Thompson and

    Dennis Ritchie, continue research. Theywere still left without a Convenientinteractive computing service*.

    * Ritchie, D.M. The Evolution of the Unix Time-sharing System, AT&T Bell Laboratories

    Technical Journal, Oct. 1984, Vol 63, No.8, Part 2, pp. 1577-1594.

  • 7/30/2019 UNIX Linux Basics Final

    9/729

    Unix Histo ry

    At the same time KenThompson wrote a gamespace travel in Fortran torun on GECOS OS

    (Honeywell 635).

    The spaceship was hardto control and it was

    expensive to run. He wastold to get the game off hiswork computer.

  • 7/30/2019 UNIX Linux Basics Final

    10/7210

    Unix Histo ry

    Thompson ported the game to a littleused PDP-7 computer.

    Unics (later Unix) was born as a punon Multics.

  • 7/30/2019 UNIX Linux Basics Final

    11/7211

    Unix Histo ryPDP-7

  • 7/30/2019 UNIX Linux Basics Final

    12/7212

    Unix Histo ry

    Dennis Ritchiedeveloped B . Thenwrote C a compiled

    language.

    In 1973 entire OSported to C.

  • 7/30/2019 UNIX Linux Basics Final

    13/72

    14

    Linus Torvalds

    1991 Linux 0.02 is first released to the public.

    1994 Linux 1.0 is released.

  • 7/30/2019 UNIX Linux Basics Final

    14/72

    Three Defini t ions o f Linux

    Linux Kernel:The very low-level software that managesyour computer hardware and provides a library (POSIX)

    interface for user-level software. The Linux kernel runs on

    many platforms (Intel x86 and IA-64, Alpha, MIPS, HP PA-

    RISC, PowerPC, IBM S/390, SPARC, Motorola 680x0, etc.).

    GNU/Linux OS:The Linux kernel plus utility software to

    provide a useful working environment.

    Linux Distributions:The packaging of the Linux Kernel,

    the GNU/Linux OS and lots of other software to make Linux

    easy to install, configure, and use (at least for the target

    audience).

  • 7/30/2019 UNIX Linux Basics Final

    15/72

    Tux , the Linux Masco t

  • 7/30/2019 UNIX Linux Basics Final

    16/72

    e ree o ware ovemen The GNU Pro jec t:www.gnu.org

    The Four Freedoms The freedom to run the program, for any purpose.

    The freedom to study how the program works,

    and adapt it to your needs. The freedom to redistribute copies so you can

    help your neighbor.

    The freedom to improve the program, and

    release your improvements to the public, so thatthe whole community benefits.

    G ?

  • 7/30/2019 UNIX Linux Basics Final

    17/72

    What is GNU?

    GNU stands for GNU's Not Unix. The three letter

    abbreviation is not a joke. It emphasizes a major proje

    of the Free Software Foundation (FSF) that really

    created the LINUX operating system with many of its

    popular tools.

    Richard Stallman created FSF, in order to encourage

    the development and use of freely redistributable code

    Freely means the freedom of redistributing your code

    under certain conditions. It does NOT mean zero

    financial cost!

    The Gnu Public License (GPL) defines the terms and

    conditions of redistributing the LINUX kernel and other

    tools that make it usable, forming a LINUX distribution.

  • 7/30/2019 UNIX Linux Basics Final

    18/72

    19

    Arch i tectu re of Unix

    Kernel: Schedules programs,Manages data/file access and storage,

    Enforces security,

    Performs all hardware access.

    Init: First program run by kernel on booting.

    Shell: Presents each user a prompt,Interprets commands typed by user,

    Executes users commands,

    Provides user/programming environment.

  • 7/30/2019 UNIX Linux Basics Final

    19/72

    Arch i tectu re of Unix

  • 7/30/2019 UNIX Linux Basics Final

    20/72

    21

    Arch i tectu re of UnixUnix f i le system

    root (/)

    bin

    devetc

    tmpvar

    home

    spool

    mail

    (users mail)

    log

    usr

    X11R6

    bin

    (users accounts)

  • 7/30/2019 UNIX Linux Basics Final

    21/72

    22

    Typ ical Direc to r ies

    / : Root of the tree. Where it starts.

    bin, sbin, usr/bin: software for the shells and

    most common Unix commands.

    dev: short for devices, holds the files necessaryto operate peripherals such as printers and

    terminals.

    home: contains the home directories of users(/export/home on sun computers).

  • 7/30/2019 UNIX Linux Basics Final

    22/72

    23

    Typ ical Direc to r ies

    tmp: holds temporary files.

    var: contains files that vary in size;

    (Mail directories, printer spool files, logs,etc.)

    etc: administrative files such as lists ofuser names and passwords.

  • 7/30/2019 UNIX Linux Basics Final

    23/72

  • 7/30/2019 UNIX Linux Basics Final

    24/72

    25

    File and Direc to ry Name Rules

    Valid names can be made up of:

    Uppercase letters (A to Z).

    Lower case letters (a to z). Case sensitive!!!

    Numbers (0 to 9). Period (.), underscore (_), commas (,).

    Should not contain spaces or the following:

    & * \ | [ ] { } $ < > ( ) # ? ; ^ ! ~ %. Never /. You should avoid naming files or directories with

    Unix commands.

  • 7/30/2019 UNIX Linux Basics Final

    25/72

    26

    Fi le Sys tem Structu re

    Unix stores a file's administrativeinformation (its physical location ondisk, permissions including ownershipand modification times) in aninode(i-

    node or Index Node).

    The file name (link) is stored in thecontents of a directory entry. Deleting

    a file consists of removing a link to theinode (the inode itself is not deleted).

  • 7/30/2019 UNIX Linux Basics Final

    26/72

    27

    Fi le System Structu re

    Data Recovery: When a file is deleted the number oflinks to the inode is reduced by one.

    Note: an inode may have more thanone link (or name) --- see ln(1).

    If the number of links becomes zero,the kernel may reuse the disk space

    making recovery difficult. MagneticForce Microscopy (MFM) can recovermost data unless wipe(1) is used.

  • 7/30/2019 UNIX Linux Basics Final

    27/72

    The K Desk top Env ironm ent (KDE)

  • 7/30/2019 UNIX Linux Basics Final

    28/72

    GNOME

  • 7/30/2019 UNIX Linux Basics Final

    29/72

    30

    OpenLook Windows Manager (o lwm )

  • 7/30/2019 UNIX Linux Basics Final

    30/72

    31

    Common Desktop Env i ronment (CDE)

  • 7/30/2019 UNIX Linux Basics Final

    31/72

    32

    How Do You Boo t Unix?Mult i -boot Prog rams

    Windows NT OS loader- select the OS ofchoice

    Linux LILO (Linux loader)-the tab key will

    show

    Selections. Type the name of the OS to load

    GrubNewer versions of Linux (i.e., 9.0) use this loaderto choose the OS

    Other methods to boot- Norton systemcommander, boot disc, Boot Magic

  • 7/30/2019 UNIX Linux Basics Final

    32/72

    Shell

    Program that interacts with kernel Bridge between kernel and the user Command interpreter

    User can type command and thecommand is conveyed to the kernel and itwill be executed

  • 7/30/2019 UNIX Linux Basics Final

    33/72

    Types o f Shel l

    Sh simple shell

    BASH Bourne Again Shell

    KSH Korne Shell

    CSH C Shell

    SSH Secure Shell To use a particular shell type the shell name at the command

    prompt.

    Eg $csh will switch the current shell to c shell

    To view the available shells in the system, type cat /etc/shells atthe command prompt

    To view the current shell that is being used, type echo $SHELL at

    the command prompt

  • 7/30/2019 UNIX Linux Basics Final

    34/72

    UNIX COMMANDS

    UNIX Commands

  • 7/30/2019 UNIX Linux Basics Final

    35/72

    UNIX Commands

    A command is a program which interacts with the kernel

    to provide the environment and perform the functionscalled for by the user.

    A command can be: a built-in shell command; anexecutable shell file, known as a shell script; or a source

    compiled, object code file.

    The shell is a command line interpreter. The userinteracts with the kernel through the shell. You can writeASCII (text) scripts to be acted upon by a shell.

  • 7/30/2019 UNIX Linux Basics Final

    36/72

    L inux Command Bas ics

    To execute a command, type its nameand arguments at the command line

    ls -l /etc

    Command nameOptions(flags)

    Arguments

  • 7/30/2019 UNIX Linux Basics Final

    37/72

    Standard Files

    UNIX concept of standard files standard input (where a command gets its

    input) - default is the terminal

    standard output (where a command writes it

    output) - default is the terminal

    standard error (where a command writes

    error messages) - default is the terminal

  • 7/30/2019 UNIX Linux Basics Final

    38/72

    Redirect ing Outpu t

    The output of a command may be sent(piped) to a file:

    ls -l >output> is used to specify

    the output file

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/ls.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ls.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    39/72

    Redirect ing Input

    The input of a command may come (bepiped) from a file:

    wc

  • 7/30/2019 UNIX Linux Basics Final

    40/72

    Common Commands

    pwd - print (display) the working directory cd- change the current working

    directory to dir ls - list the files in the current working directory ls -l - list the files in the current working

    directory in long format

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/pwd.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/cd.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ls.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ls.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ls.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ls.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/cd.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/pwd.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    41/72

    Fi le Commands

    cp Copy from the to the

    mv Move/rename the to the

    rm Remove the file named

    mkdir Make a new directory called

    rmdir

    Remove an (empty) directory

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/cp.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/mv.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/rm.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/mkdir.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/mkdir.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/mkdir.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/mkdir.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/rm.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/mv.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/cp.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    42/72

    More Commands

    who List who is currently logged on to the system

    whoami Report what user you are logged on as

    ps List yourprocesses on the system

    ps aux List all the processes on the system

    echoA string to be echoed Echo a string (or list of arguments) to the terminal

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/who.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/whoami.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ps.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ps.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/echo.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/echo.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ps.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ps.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/whoami.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/who.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    43/72

    More Commands

    alias - used to tailor commands: alias erase=rm

    alias grep=grep -i

    ar- Maintain archive libraries: acollection of files (usually object fileswhich may be linked to a program, like aCMS TXTLIB)

    M C d

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/alias.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ar.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/ar.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/alias.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    44/72

    More Commands

    awk -a file processing language that iswell suited to data manipulation andretrieval of information from text files

    chown -sets the user ID (UID) to ownerfor the files and directories named bypathname arguments. This command is

    useful when from test to production

    M C d

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/awk.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/chown.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/chown.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/awk.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    45/72

    More Commands

    diff- attempts to determine theminimal set of changes needed toconvert a file specified by the firstargument into the file specified by thesecond argument

    find -Searches a given file hierarchyspecified by path, finding files that match

    the criteria given by expression

    M C d

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/diff.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/find.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/find.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/diff.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    46/72

    More Commands

    grep -Searches files for one or morepattern arguments. It does plain string,basic regular expression, and extendedregular expression searching

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/grep.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/grep.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    47/72

    More Commands

    kill - sends a signal to a process orprocess group

    You can only kill your own processes

    unless you are root

    UID PID PPID C STIME TTY TIME CMD

    root 6715 6692 2 14:34 ttyp0 00:00:00 sleep 10h

    root 6716 6692 0 14:34 ttyp0 00:00:00 ps -ef

    [root@penguinvm log]# kill 6715

    [1]+ Terminated sleep 10h

    M C d

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/kill.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/kill.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    48/72

    More Commands

    make - helps you manage projectscontaining a set of interdependent files(e.g. a program with many source and

    object files; a document built from sourcefiles; macro files) make keeps all such files up to date with

    one another: If one file changes, make

    updates all the other files that depend onthe changed file Roughly the equivalent of VMFBLD

    M C d

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/make.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/make.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    49/72

    More Commands

    sed -applies a set of editingsubcommands contained in a script to eachargument input file

    find ./ -name "*.c,v" | seds/,v//g | xargsgrep "PATH"

    This finds all files in the current and subsequent directories with an extension of c,v. sed then

    strips the ,v off the results of the find command. xargs then uses the results ofsed and builds agrep command which searches for occurrences of the word PATH in the C source files.

    Pi

    http://linux.com.hk/man/showman.cgi?manpath=/man/man1/sed.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/find.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/sed.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/xargs.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/grep.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/grep.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/xargs.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/sed.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/find.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/find.1.inchttp://linux.com.hk/man/showman.cgi?manpath=/man/man1/sed.1.inc
  • 7/30/2019 UNIX Linux Basics Final

    50/72

    Pipes

    An important early development in Unix was theinvention of "pipes," a way to pass the output ofone tool to the input of another.

    eg. $ who | wc l

    By combining these two tools, giving the wccommand the output of who, you can build a newcommand to list the number of users currently onthe system

    Basic Unix commands (vs. DOS)

  • 7/30/2019 UNIX Linux Basics Final

    51/72

    Basic Unix commands (vs. DOS)

    cat file Concatenate or type out a file,

    cat file1 file2... Type out a number of files

    cd directory1 Change current directory to directory1cd /usr/bin Change current directory to /usr/bin

    cd Change back to your home directory

    clear Clear the current screen

    cp file1 file2 Copy file1 to file2

    cp file1 file2... dirCopy a number of files to a directory

    ls List the files in the current directory

    ls /usr/bin List the files in the /usr/bin directory

    lpr file1 Print file1 out , lprfile1 file2 .. Print a number of files out

    more file Look at the content of a file with paging, use q to get out

  • 7/30/2019 UNIX Linux Basics Final

    52/72

    Basic Unix commands (vs. DOS)

    mkdir directory Create a directory

    mvfile1 file2 Movefile1 tofile2, like rename.

    mvfile1 file2 ... dirMove a number of files into a directory

    mv dir1 dir2 Move or rename a directory

    rm file Remove a file

    rm file1 file2... Remove a number of files

    rm -r directory Remove a directory include the sub-directory

    rmdirdirectory Remove a directory

  • 7/30/2019 UNIX Linux Basics Final

    53/72

    Basic Unix commands (vs. DOS)

    DOS Command Unix

    CommandDescriptions

    CDCHKDSK

    CLS

    COPY

    DEL

    DIRMD

    MOREPRINT

    RDRENAME

    TYPE

    cdduclear

    cp

    rm

    lsmkdirmorelprrmdirmv

    cat

    Change directoryDisk usageClear the current screen

    Copying filesRemoving files or directoriFile listing of directories

    Create a directory

    Type out a file with pagingPrint out a fileRemove a directory

    Moving fi les around

    Type out files

  • 7/30/2019 UNIX Linux Basics Final

    54/72

    Who, When, Why , What and Where?

    man cp Display on-line manual for the cp command

    man -kkeyword Display manual help file related to the keyword

    passwd Change your login password

    pwd Display the path name of where you are

    uptime Tell you how long the machine has been up and running

    users Tell you who is logging in

    who Tell you who is logging-in in detail

    w Tell you who is logging in and doing what!

    whoami Show you the owner of this account

    fingeruser Find out the personal information of a user

    fingername Try to find the persons info. by his/her name

    fingeremail-address Try to find the persons info across the network

    write user Write a message on somebodys screen

    talkuser Talk to the person logging in the same system with you

    talkemail-address Talk to somebody logging in the network

    date Display todays time and date

    calyear Display the calendar of the specified year (e.g. 1997)

    Sh l l S i t i

  • 7/30/2019 UNIX Linux Basics Final

    55/72

    Shel l Scr ipt ing

    Shell scripting is the most useful andpowerful feature in Linux

    Minimizes typing of repetitive command

    Can schedule jobs to run in the system Can initiate back up activities for system

    administration

    Similar to batch files in DOS, but more

    powerful than Batch files

    W ki i th h l l i t

  • 7/30/2019 UNIX Linux Basics Final

    56/72

    Working w i th shel l sc r ip t

    Open a file with extension .sh using vieditor

    We can type any number of commandsthat we use to type at command prompt

    Save the file Execute the file

    sh file.sh

    ./file.sh (if the file has execution permission)

    VI Edito r

  • 7/30/2019 UNIX Linux Basics Final

    57/72

    Popular text editor Just type vi at the prompt

    and hit the enter key.

    A new file will be opened Type the contents needed and save To save, press the Esc Key and then

    press : (colon) w q and then enter

    To quit with out saving Esc + : + q andthen enter

    Vi d it

  • 7/30/2019 UNIX Linux Basics Final

    58/72

    Vi ed ito r

    Navigation Left - h Down - j Up - k Right - l Top of the screen H (shift + h) //caps lock will not work Middle of the screen M (shift + m) Bottom of the screen L (shift + l) $ - End Key, 0 Home Key

    Edit Commands Cut X, x

    Copy yy, yw Paste P, p

    Vi ed ito r

  • 7/30/2019 UNIX Linux Basics Final

    59/72

    Xcuts the letter after cursor positionxcuts the letter before cursor position

    yyCopies a line at which cursor is currently positioned

    ywCopies a word at which cursor is currently positioned

    PPastes the copied content after cursor position

    pPastes the copied content before cursor position

    ddDeletes a line

    Vi ed ito r

    Shel l Scrip ts

  • 7/30/2019 UNIX Linux Basics Final

    60/72

    Shel l Scrip ts

    To Print a line echo Hello World (Prints Hello World in the screen)

    To read a line read n (Stores the content entered by user in variable

    n

    To Comment a line # This is a comment Only single line comment is available. For multi line

    comment, we need to use # symbol in lines which wewant to comment.

    Condi t ions

  • 7/30/2019 UNIX Linux Basics Final

    61/72

    Condi t ions We can have if, if else, if elif else and case statements (Nested if

    statements are also possible

    1. if [ condn ]then

    fi

    2. if [ condn ]then

    elsefi

    3. if [ condn ]then

    elif [ condn ]then

    elsefi

    Condit ions (Case)

  • 7/30/2019 UNIX Linux Basics Final

    62/72

    Condit ions (Case)

    case expr in

    Option1) stmt ;;Option2) stmt ;;*) stmt ;;esac

    Every option should be terminated with adouble semicolon.

    Denotes default case

    Case should be termniated with esac

    Loops

  • 7/30/2019 UNIX Linux Basics Final

    63/72

    Loops

    For loopfor i in 1 2 3 4 5 //Loops 5 times

    do

    Body of the loopdone

    for (( i=0; i

  • 7/30/2019 UNIX Linux Basics Final

    64/72

    Whi le Loop

    while [ condn ]do

    body of the loop

    done

    We need to ensure that the while loopcondition is terminated in a finite way

    Compar ison

  • 7/30/2019 UNIX Linux Basics Final

    65/72

    Compar ison

    For integer comparison we have the

    following-eq : equal to

    -ne : not equal to

    -lt : less than

    -gt : greater than

    -le : less than or equal to

    -ge : greater than or equal to

    Compar ison

  • 7/30/2019 UNIX Linux Basics Final

    66/72

    Compar ison

    For string comparison we have- = : equal to

    - ~= : not equal to

    For logical operators-a : AND

    -o : OR

    Arrays

  • 7/30/2019 UNIX Linux Basics Final

    67/72

    Arrays

    Initialising an array A[0] = 10 A[1] = Hi

    Using an array ${A[0]} : retrieves 10

    Here arrays can contain data belonging todifferent data types

    Uninitialised index in arrays will have null valueby default

    Funct ions

  • 7/30/2019 UNIX Linux Basics Final

    68/72

    Funct ions

    Local Function Written at the command prompt Lasts for the current session alone Global Function

    Written in .bashrc file Available as long as the definition is there in

    .bashrc file Function in shell script

    Available with in the file alone

    Parameters

  • 7/30/2019 UNIX Linux Basics Final

    69/72

    Parameters

    Sample functionFunctionname()

    {echo $1

    }

    Calling function:Functionname Ram

    Result:Ram

  • 7/30/2019 UNIX Linux Basics Final

    70/72

    We can pass any number of parameters like this

    $0File name

    $1,$2 - first, second parameters and so on

    $?stores the status (success or failure status of the last command that was

    executed)

    $# - stores the number of parameters passed through the command line

    #pwd

    /home

    #echo $?

    0 // As the last command is success

    #abcError:command not found

    #echo $?

    127 // as the last command didnt execute successfully

    C Prog ram in Linux

  • 7/30/2019 UNIX Linux Basics Final

    71/72

    C Prog ram in Linux

    Open a file with extension .c from the command prompt using vi

    editor

    vi hello.c

    Type the contents and save (Esc : wq!)

    Compile the file

    gcc hello.c Run the executable

    ./a.out

    Compile file with output option

    gcco hello hello.c

    Run the executable

    ./hello

  • 7/30/2019 UNIX Linux Basics Final

    72/72