51
Unix/Linux basics 0011 Operating systems lab Gergely Windisch [email protected] room 4.12 http://nik.bmf.hu/gwindisch/ os_2010

Unix/Linux basics 0011 Operating systems lab Gergely Windisch [email protected] room 4.12

Embed Size (px)

Citation preview

Page 1: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Unix/Linux basics0011

Operating systems labGergely Windisch

[email protected] 4.12

http://nik.bmf.hu/gwindisch/os_2010

Page 2: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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?

Page 3: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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" > ~/truthcp truth t2cp truth t3mv t2 t4ln t5 t3ln t4 /etc/t6mv /etc/t6 ~/t7ln /etc/t7 t8

What are the inode numbers for truth, t2, t3, t4, t5, t6, t7 t8?

Page 4: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Control structures if [ logical_expression ]

then...commands...

elif [ logical_expression_2 ]then

...commands...else

...commands...fi

Page 5: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 6: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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 ].

Page 7: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Case case ”variable” in

”string1”) commands;;”string2”) commands;;*) commands;;esac

apple=1case ”$apple” in”1”) ... ;;”2”) ... ;;esac

Page 8: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

for

for iteration_variable in listdo

commandsdone

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

Page 9: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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 commandsdone

seq 10 returns: 1 2 3 4 5 6 7 8 9 10

Page 10: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

commands

done while [ ”$K” -ne ”3” ]; do

commands

done

Page 11: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Input from user

read read K read -n1 K : takes only one character (without

return)

Page 12: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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).

Page 13: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution #!/bin/bash

echo ”You have given me: ” case ”$1” in

”1”) echo ”one”;;”2”) echo ”two”;;...*) echo ”too large”

esacexit 0

Page 14: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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.

Page 15: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 16: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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.

Page 17: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 18: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Exercise

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

Page 19: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution

for i in `ls`do

cat $idone

Page 20: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Exercise

Create a shell script that prints out all the input parameters the user has entered in the form of:

parameter 1 is: oneparameter 2 is: two etc.

Page 21: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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).

Page 22: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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)

Page 23: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution

for i in `ls`do

echo ”File: $i”done

Page 24: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Exercise

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

Page 25: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Hint

use the file command to get the type of the file

Page 26: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution #!/bin/bash

allthetext="newfile"echo `date` > $allthetextfor 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 fidone

Page 27: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Exercise

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

Page 28: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 29: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

User management

username passwords UserID GroupID

Page 30: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 31: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Creating groups

• groupadd gname• groupadd -g – add user definied GID (group ID) - usually above

1000

Page 32: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 33: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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!

Page 34: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 35: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Exercise

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

Page 36: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 37: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 38: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Exercise 6.5

• Use tar to compress all your shell scripts into one file

Page 39: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution to Exercise 6.5

tar -cvzf myShellScripts.tar.gz *

Page 40: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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)

Page 41: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution to Exercise 7

#!/bin/bashsum=`expr $1 + $2`echo "The sum of $1 and $2 is $sum"

Page 42: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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.

Page 43: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution to Exercise 8

#!/bin/bashnewfile="~/uptime"date >> $newfileuptime >>$newfileecho "----------------" >>$newfileecho " " >>$newfile

Page 44: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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.

Page 45: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution to Exercise 9

#!/bin/bashln -s $1 $2

Page 46: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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.

Page 47: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution to Exercise 10

#!/bin/bashecho "Contents of \$PATH: $PATH" > $1

Page 48: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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

Page 49: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Solution to Exercise 11

#!/bin/bashPATH="$PATH:$1"

Page 50: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

Further exercises

• http://nik.uni-obuda.hu/gwindisch/os_2010/4_1.txt• http://nik.uni-obuda.hu/gwindisch/os_2010/4_2.txt• http://nik.uni-obuda.hu/gwindisch/os_2010/4_3.txt

Page 51: Unix/Linux basics 0011 Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12

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 [email protected]:~/

• (password: hallgato)

• Or via email