Unix/Linux basics 0011 Operating systems lab Gergely Windisch room 4.12

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Unix/Linux basics user management Operating systems lab Gergely Windisch room 4.12
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Jump to first page Unix Commands Monica Stoica Jump to first page Introduction to Unix n Unix was born in 1969 at Bell Laboratories, a research subdivision.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Lesson 22 – Introduction to Linux Systems Administration.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
L INUX C OMMAND L INE I NTERFACE G UNAANBAN.G
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Unix/Linux basics 0010 Operating systems lab Gergely Windisch uni-obuda.hu room 4.12.
Brief introduction to UNIX A. Emerson CINECA, High Performance Systems.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
Linux Operations and Administration
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.
ITI-481: Unix Administration Meeting 3 Christopher Uriarte, Instructor Rutgers University Center for Applied Computing Technologies.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Users Greg Porter V1.0, 26 Jan 09. What is a user? Users “own” files and directories Permission based on “ownership” Every user has a User ID (UID) 
There are three types of users in linux  System users: ?  Super user: ?  Normal users: ?
Managing Users  Each system has two kinds of users:  Superuser (root)  Regular user  Each user has his own username, password, and permissions that.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter 3 & 6 Root Status and users File Ownership Every file has a owner and group –These give read,write, and execute priv’s to the owner, group, and.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Shell Control Statements and More
Linux Commands C151 Multi-User Operating Systems.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Manually Creating a New User Account Presented by Carl South.
1 Lecture 2 Working with Files and Directories COP 3353 Introduction to UNIX.
Sed. Class Issues vSphere Issues – root only until lab 3.
Lab 8 Overview Apache Web Server. SCRIPTS Linux Tricks.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Unix Lab Fall Shell Scripting ●Through the shell (LXTerminal) you can: ●Run programs. ●Interact with the file system. ●Change settings. ●Send/receive.
Experiment No 4 Prepared by, Mr. Satish Pise. Objectives View the /etc/passwd file and describe its syntax. View the /etc/shadow file and describe its.
Linux Essentials Chapter 12: Creating Scripts. Chapter 12 Outline Beginning a shell script Using commands Using arguments Using variables Using conditional.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Using Linux Kaya Oğuz Room: 310.
Bash Shell Scripting 10 Second Guide.
Chapter 11 Command-Line Master Class
Prepared by: Eng. Maryam Adel Abdel-Hady
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Shell Scripting March 1st, 2004 Class Meeting 7.
Shell Script Assignment 1.
Using Linux Commands Lab 3.
Linux Users and Groups Management
LING 408/508: Computational Techniques for Linguists
UNIX Reference Sheets CSE 2031 Fall 2010.
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

Unix/Linux basics 0011 Operating systems lab Gergely Windisch room

Permissions (quiz)‏ touch file1 chmod 354 file1 chmod a-X file1 chmod ug+r file1 chmod o-w file1 chmod g-w file1 What is the result? Octal number? Textual representation?

inode (quiz)‏ Let's assume that we have two disks. Disk1 and Disk2. Disk1 holds /, Disk2 holds /home. Assume that the inode numbers increase by one each and every time a new inode is created. The next free inode number is 345 on Disk1 and 763 on Disk2. We run the following commands: cd ~ echo "inodes rule" > ~/truth cp truth t2 cp truth t3 mv t2 t4 ln t5 t3 ln t4 /etc/t6 mv /etc/t6 ~/t7 ln /etc/t7 t8 What are the inode numbers for truth, t2, t3, t4, t5, t6, t7 t8?

Control structures if [ logical_expression ] then...commands... elif [ logical_expression_2 ] then...commands... else...commands... fi

Logical expressions if [ -r filename ] : switches to analize files -r : see if file exists and readable -w: file exists and writeable... man test Comparing numbers: -eq instead of ==  $n1 -eq $n2  $n1 -ne $n2, gt, ge, lt, le

Logical expressions 2 Comparing strings  if [ ”string1” == ”string2” ] then echo... fi string1 and 2 could be $variable1 and $variable2 white spacing matters!!! spaces must be places around [, == and ].

Case case ”variable” in ”string1”) commands;; ”string2”) commands;; *) commands;; esac apple=1 case ”$apple” in ”1”)... ;; ”2”)... ;; esac

for for iteration_variable in list do commands done cycle steps over the elements of lists list: "one two three four five six seven"  a list of words divided by SPACE  can be written as a string or generated by a command

for (continued) In BASH scripts we usually use for to iterate over lists of files use seq to have a "convetional" for loop  for i in `seq 30` do commands done seq 10 returns:

Until, while until [ ”$K” -eq ”3” ]; do commands done while [ ”$K” -ne ”3” ]; do commands done

Input from user read read K read -n1 K : takes only one character (without return)

Exercise 1 Create a shell script which takes a number as an input parameter and then writes back in English the „name” of that number. If the given number is not between 0 and 9, it should say: out of bounds (or any other error message you find amusing).

Solution #!/bin/bash echo ”You have given me: ” case ”$1” in ”1”) echo ”one”;; ”2”) echo ”two”;;... *) echo ”too large” esac exit 0

Exercise 2 Create a shell script that shows a list of options to the user. If the user presses 1, the program should list contents of the current working directory. Pressing two tells the user the name of the current directory, and if the user presses 3, the program should quit. There should be an error message for any other keys.

Solution #!/bin/bash K=0 echo '**********************' echo '* Menu *' echo '* 1 - Pritnt name *' echo '* 2 - Contents *' echo '* 3 - Quit *' echo '**********************' echo Please choose one: read K case "$K" in "1") ls -l uptime;; "2") pwd;; "3") echo Bye!;; *) echo "Choose one between 1 and 3";; esac

Exercise 3 Create a shell script that takes the name of a file as an input parameter and a string that should be inserted in that file. The string can be omitted, in that case the program should ask for it from the user. Once it has the name of the file and the string, it should append the string to the end of the file, but only if the said file exists, is a text file and is writeable. If not, give a customized error message. If the program is started without parameters, it should print usage information.

Hints for Exercise 3 first input parameter: $1 second input parameter: $2 number of input parameters: $# asking the user something: read varname man test to find out how to check for writeable files appending example: echo "$something" >> file

Exercise Create a program which prints the content of all the files in the current directory. Use for.

Solution for i in `ls` do cat $i done

Exercise Create a shell script that prints out all the input parameters the user has entered in the form of: parameter 1 is: one parameter 2 is: two etc.

Exercise Create a user friendly shell script that requires an input string which should be appended to a file. The file name and the string should come as parameters. If -v is an input parameter, it should do it's job verbosely (tell the user what it is doing at any given point). If -h is given, then don't do anything but return some helpful information (the same effect should come when there are fewer than two parameters).

Exercise Write a shell script that prints the content of the current directory in the form of: ”File: name_of_file” (bonus points: try to separate directories from files)

Solution for i in `ls` do echo ”File: $i” done

Exercise Create a shell script that copies the contents of all the ASCII text files into one big file.

Hint use the file command to get the type of the file

Solution #!/bin/bash allthetext="newfile" echo `date` > $allthetext for i in `ls` do something=`file $i` something2="$i: ASCII text" if [ "$something" == "$something2" ] then if [ "$allthetext" != "$i" ] then # echo "$i is a text file" cat $i >> $allthetext fi fi done

Exercise Add commands to the program with the menu, so that it really wouldn't quit unless the user presses 3

Solution #!/bin/bash K=0 until [ "$K" -eq "3" ]; do echo '**********************' echo '* Menu *' echo '* 1 - List *' echo '* 2 - Current dir *' echo '* 3 - Quit *' echo '**********************' echo Please choose one: read K case "$K" in "1") ls -l uptime;; "2" pwd;; "3") echo Bye!;; *) echo "Choose between 1 and 3!";; esac done

User management username passwords UserID GroupID

User management /etc/passwd : user information (clear text)‏ /etc/shadow : passwords (encoded)‏ /etc/group : groups – take a look at these groupadd : create groups useradd: create users /home/username: home of the new users – May not get created by default /etc/default/useradd /etc/skel - default directory structure Creating a user in linux can be done by modifying a few files

Creating groups groupadd gname groupadd -g – add user definied GID (group ID) - usually above 1000

Creating users useradd user1 – create user (without settings)‏ useradd -D : print the defaults useradd -g group1 user3 – primary group useradd -g group1 -G gr2,gr3,gr4 user4 – secondary groups useradd -m user5 – create home directory automatically useradd -p password user6 – create password

Creating users - hardcore way add a new line to /etc/passwd create the home directory for the new user use command passwd username to create a password Try the new user!

Additional commands passwd – modify our password passwd usernev – modify the password of an other user passwd root - way to "create" root in ubuntu groupdel userdel

Exercise Create two new users. One using commands, the other by modifying the passwd file

Change ownership files have owners chown user.group file1 – give your file to someone else try it with new new users – use alt+f2 - f6 to switch between the consoles

Compression tar (tape archiever) tar -cvzf nameoffile.tar.gz * – pack and compress everything in cwd tar -xvzf nameoffile.tar.gz – unpack the contents of nameoffile.tar.gz Switches – man tar – x: eXtract, c: create, v: verbose, z: gzip, f: filename

Exercise 6.5 Use tar to compress all your shell scripts into one file

Solution to Exercise 6.5 tar -cvzf myShellScripts.tar.gz *

Exercise 7 Create a shell script which takes two numbers as input parameters and add them together (those of you who already know ifs should write a full-featured calculator)

Solution to Exercise 7 #!/bin/bash sum=`expr $1 + $2` echo "The sum of $1 and $2 is $sum"

Exercise 8 Write a shell script that adds the current date (date command) and the current uptime (uptime command) to the ~/uplog file. There should be a separator after these two data, so that the next run can be identified easily.

Solution to Exercise 8 #!/bin/bash newfile="~/uptime" date >> $newfile uptime >>$newfile echo " " >>$newfile echo " " >>$newfile

Exercise 9 Write a shell script that takes two input parameters from the user, and then creates a symbolic link pointing to the file denoted by the first parameter with the name provided as the second parameter.

Solution to Exercise 9 #!/bin/bash ln -s $1 $2

Exercise 10 Write a shell script that prints the contents of the PATH (the one which holds the names of the directories where the shell would look for an executable) variable to a file. The filename is provided in the first input parameter.

Solution to Exercise 10 #!/bin/bash echo "Contents of \$PATH: $PATH" > $1

Exercise 11 Write a shell script that takes the name of a directory as an input parameter and adds that directory to the PATH variable. Be careful not to overwrite the original content, just add it (and use the standard separator of that variable). Advanced versions (require for and if) – check if the directory exists – if the user provides more than one dir, add them as well

Solution to Exercise 11 #!/bin/bash PATH="$PATH:$1"

Further exercises

Upload your precious work If you want, you can send your work to me. If the solutions are correct (or interesting), it could count in your final grades. Send it using the commands: – sudo dhclient eth2 (password: nik119) – scp name_of_tar (password: hallgato) Or via