+ All Categories
Home > Documents > Introduction to Linux and Shell Scripting Jacob Chan.

Introduction to Linux and Shell Scripting Jacob Chan.

Date post: 24-Dec-2015
Category:
Upload: robyn-wiggins
View: 251 times
Download: 0 times
Share this document with a friend
Popular Tags:
37
Introduction to Linux and Shell Scripting Jacob Chan
Transcript

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

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)

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)

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>

Review on Linux Commands

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

<command> <file> > <newFile>

<command> < <file>

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?

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

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>

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

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)

Shell Script

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

#!/bin/bash

pwd

ls –l

echo “hello world”

touch pogi.txt

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!

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>

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

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

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”

Shell Programming

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

echo “$foo”

echo \$foo

echo $’foo’

echo $foo

Shell Programming

• Conditionals– If Statements

if command1

then

command2

fi

if command1; then command2; fi

if command1; then

command2

else

command3

fi

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)

Shell Programming

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

if command1; then

command2

elif command3; then

command4

else

command5

fi

Shell Programming

• Conditionals– The case statement (no switch required)

case WORD in

(PATTERN) commands ; ;

(PATTERN) commands ; ;

(PATTERN) commands ; ;

esac

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)

Shell Programming

• Loops– Example of for loop

for file in /etc/*

do

echo “${file}”

done

Shell Programming

• Loops– While loop

while [ condition ]

do

command1

command2

command3

done

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

Shell Programming

• Reading input– Use either the read command– Example

#!/bin/bash

echo “what is your name?”

read name

echo “hello $name”

Shell Programming

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

chmod +x <file>

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)

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

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:

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:

LAB 1: Printing File Details

• Symbolic LinkFile Name:

File Type:

Link to Pathname?

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:

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

NEXT MEETING

• C Basics• More programming and LAB

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


Recommended