Top Banner
Introduction to Linux and Shell Scripting Jacob Chan
37
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Linux and Shell Scripting Jacob Chan.

Introduction to Linux and Shell Scripting

Jacob Chan

Page 2: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Windows vs Linux– Stability

• Windows often crashes; Linux rarely does• Bug potential

– Cost– Learnability– Interface– Market prowess

• Windows for games, Linux for industry• Windows’ customer service features

– TIP: DON’T ALWAYS RELY ON THIS

Page 3: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Some important commands in Linux– File commands

file <filename> (print file type)

stat <filename> (print more detailed file information)

cat <filename> (view file contents)

touch <filename> (create new blank file)

gedit <filename> (create new blank file, but it is not yet saved

– Directory commands

mkdir

cd <directory>

ls <argument> <directory> (argument can be –l, -I, -a)

Page 4: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Some important commands in Linux– System commands

exit

pwd (print working directory)

who (displays currently logged in users)

whoami (current user displayed)

– Manual pagesman <command> (opens manual page of command)

info <command> (works like man but has link to other pages)

Page 5: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Some important commands in Linux– File data manipulation

wc

grep

sort

head

cut

– File accesschmod <permission> <file>

umask <permission>

ln –s <file> <link>

Page 6: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Other commands– Regular Expressions (*, [0-9], ?)– I/O Redirection

<command> <file> > <newFile>

<command> < <file>

Page 7: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Other commands– Pipes | are used for combining different commands at once– Example: Create a file containing the top 10 foods you like– Then, type this command:

cat food.txt | grep “b” | sort

What happens?

Page 8: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Running processes in the background– Sometimes, when we run gedit, we can’t type on the command

line– Solution: put &– This way, you can work on your text file AND type at the

command line at the same time

Page 9: Introduction to Linux and Shell Scripting Jacob Chan.

Review on Linux Commands

• Processes– There is an ID for every process running– Viewing PID’s

ps -ax- Killing processes (terminates the process no matter what)

kill -9 <pid>

Page 10: Introduction to Linux and Shell Scripting Jacob Chan.

Now What?

• Typing all these commands is a hassle• Piping is a solution, but you don’t want to pipe a lot of

commands together!• This is especially true in handling multiple files at the

same time– Database management– Running executables– Backups

Page 11: Introduction to Linux and Shell Scripting Jacob Chan.

A more efficient way: SHELL

• /bin/bash or /bin/sh (Bourne again shell)• Automates processes that are typed over and over• Works like .bat files (for Windows users)

– But Windows has a tendency to make “weird” changes from those .bat files– In Linux/Unix, you won’t get that

• Shell files are special text files– Usually ends in .sh– First line: #!/bin/bash

• Helps in identifying that the file is shell script (comment)

Page 12: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Script

• Typical content of shell– Text– Write first shell script

#!/bin/bash

pwd

ls –l

echo “hello world”

touch pogi.txt

Page 13: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Script

• Running shell– Try typing the shell file first.– Then try typing ./before writing the shell file– Reason why latter worked: system issues

• The shell script is not included in the path variable, so it won’t work by default

• Same goes with executables

Good job on your first shell script!

Page 14: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Variables– Some variables are pre-defined by the system (RESERVED)

• PATH (list of directories searched when system looks for command)• TERM (name of current terminal)• HOME (user’s home directory)• LOGNAME (user’s login name)• SHELL (name of shell program)

– How to use variables

a = <something>

Page 15: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Variables– Access variable value

• Use $ or ${}

– Access program output• Use $(programName) or “ ‘programName’ “

– Displaying value of variables:echo $PATH

echo ${PATH}

– Variables can either be used as command name or argumentcp $file ${file}02$CC myprog.c -o myprog

Page 16: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Accessing program output examplelogin=$(cat /etc/passwd | grep user| cut -d: -f1)echo ${login}today=`date | cut -d\ -f1-2`echo $todayecho ${today} > /tmp/today.txt

Page 17: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Quoting– These characters have special meaning for shell (space, &, < >

$ * “ ‘)– How to go around them: \

cat Your\ Mom

Mkdir food\&bar

– Single quotes suppresses special meanings

Example: help=‘Donuts $35.00 & Brownies $20.00’– BUT double quotes won’t suppress meaning of $

Example: PATH = “$Path:/home”

Page 18: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Quoting Test– Given that foo = “bar”, what is the result of the following:

echo “$foo”

echo \$foo

echo $’foo’

echo $foo

Page 19: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Conditionals– If Statements

if command1

then

command2

fi

if command1; then command2; fi

if command1; then

command2

else

command3

fi

Page 20: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Conditionals– example of if statement

read ansif [ “$ans” = “y” ]; then echo “Your answer is yes”else echo “Your answer is not yes”fi

The [] command is like the test command (like a shortcut for checking booleans)

Page 21: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Conditionals– The “normal” if (the one we usually know)

if command1; then

command2

elif command3; then

command4

else

command5

fi

Page 22: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Conditionals– The case statement (no switch required)

case WORD in

(PATTERN) commands ; ;

(PATTERN) commands ; ;

(PATTERN) commands ; ;

esac

Page 23: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Loops– For loops

for <item> in <list>; do

commands

done

#will check for each item in list

#useful for processing each file in directory

#list is either a command in Linux, a list (or sequence) of numbers or a plain list

#curly braces are optional for list (and are used for numbers only)

Page 24: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Loops– Example of for loop

for file in /etc/*

do

echo “${file}”

done

Page 25: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Loops– While loop

while [ condition ]

do

command1

command2

command3

done

Page 26: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Loops– Until loop (works like while except that statement will be executed until condition

is TRUE (therefore, it will execute when FALSE)

until [ condition ]

do

command1

command2

command3

done

Page 27: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Reading input– Use either the read command– Example

#!/bin/bash

echo “what is your name?”

read name

echo “hello $name”

Page 28: Introduction to Linux and Shell Scripting Jacob Chan.

Shell Programming

• Scripts are executable files. So to run them, make the file executable. How?

chmod +x <file>

Page 29: Introduction to Linux and Shell Scripting Jacob Chan.

PROGRAMMING EXERCISE

• Try creating a program that displays the date today, and checks if the time now is in the morning, afternoon, or evening. (6am to 12pm is morning, 12pm to 6pm is afternoon, 6pm to 6am is night). Print the appropriate greeting (Good morning, good afternoon, good evening)

• Clue: find a way on how to print out the date (it was already discussed a while ago)

Page 30: Introduction to Linux and Shell Scripting Jacob Chan.

LAB 1: Printing File Details

• Create a lab filedetails.sh that describes a file as detailed as possible. The command will be run as

./filedetails.sh NAME

where NAME is the name of the file we want details of.• Wildcards will also be considered, meaning I can type

./filedetails.sh * (which prints out details of every file• Note: you also need to know how to get file types (if it is

a device file, regular file, directory, or a symbolic link

Page 31: Introduction to Linux and Shell Scripting Jacob Chan.

LAB 1: Printing File Details

• Regular Files (and other file types)File Name:

File Type:

Owner:

Permissions for Owner:

Group:

Permissions for Group:

Permissions for Others:

Actual size:

Size on Disk:

Inode No.:

N of Links:

Access Date/Time:

Modify Date/Time:

Change Date/Time:

Page 32: Introduction to Linux and Shell Scripting Jacob Chan.

LAB 1: Printing File Details

• Device FilesFile Name:

File Type:

Owner:

Permissions for Owner:

Group:

Permissions for Group:

Permissions for Others:

Block or Char?

Device Major:

Device Minor:

Inode No.:

N of Links:

Access Date/Time:

Modify Date/Time:

Change Date/Time:

Page 33: Introduction to Linux and Shell Scripting Jacob Chan.

LAB 1: Printing File Details

• Symbolic LinkFile Name:

File Type:

Link to Pathname?

Page 34: Introduction to Linux and Shell Scripting Jacob Chan.

LAB 1: Printing File Details

• DirectoriesFile Name:

File Type:

Owner:

Permissions for Owner:

Group:

Permissions for Group:

Permissions for Others:

Inode No.:

N of Links:

Access Date/Time:

Modify Date/Time:

Change Date/Time:

Page 35: Introduction to Linux and Shell Scripting Jacob Chan.

LAB 1: Printing File Details

• Deadline of submission: NEXT WEEK– Links are provided for Sections A and B.

• Put your filedetails.sh in a folder, along with a CERTIFICATE OF AUTHORSHIP (DO NOT FORGET THIS)

• Filename: CS162B_<Section>_Lab1_<Surname>_<IDNumber>.tar

• Submit on the link provided for each class. Each link I will post a password

Page 36: Introduction to Linux and Shell Scripting Jacob Chan.

NEXT MEETING

• C Basics• More programming and LAB

Page 37: Introduction to Linux and Shell Scripting Jacob Chan.

THE ENDNo dream is too big and no dreamer is too small