~/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?"> ~/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?">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Unix/Linux basics 0011 Operating systems lab Gergely Windisch room 4.12"— Presentation transcript:

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

2 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?

3 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?

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

5 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

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

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

8 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

9 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: 1 2 3 4 5 6 7 8 9 10

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

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

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

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

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

15 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

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

17 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

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

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

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

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

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

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

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

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

26 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

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

28 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

29 User management username passwords UserID GroupID

30 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

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

32 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

33 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!

34 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

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

36 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

37 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

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

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

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

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

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

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

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

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

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

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

48 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

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

50 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

51 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 hallgato@10.4.1.6:~/ (password: hallgato) Or via email


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

Similar presentations


Ads by Google