Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs288 Intensive Programming in Linux

Similar presentations


Presentation on theme: "Cs288 Intensive Programming in Linux"— Presentation transcript:

1 Cs288 Intensive Programming in Linux
Instructor: C F Yurkoski Section web site: Class 13 6-9-15

2 this weeks xkcd comic cfy

3 this week Revu Homework Python? cfy

4 Revu: These are not the same: afsconnect1-57 ~ >: echo "1 2 3"
6-9-15

5 | & ; ( ) < > space tab
metacharacter A character that, when unquoted, separates words. One of the following: | & ; ( ) < > space tab 6-9-15

6 A token that performs a control function.
control operators A token that performs a control function. It is one of the following symbols: || & && ; ;; ( ) | |& <newline> 6-9-15

7 do done elif else esac fi for function if in select then until
RESERVED WORDS Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command word of a case or for command: ! case do done elif else esac fi for function if in select then until while { } time [[ ]] 6-9-15

8 redirection > >>
< command < file.txt > out.txt 2> error.txt cfy

9 Essential commands echo cat cut tr grep man let ls wc shift cfy

10 ECHO(1) User Commands ECHO(1)
NAME echo - display a line of text SYNOPSIS echo [SHORT-OPTION]... [STRING]... echo LONG-OPTION DESCRIPTION Echo the STRING(s) to standard output. -n do not output the trailing newline -e enable interpretation of backslash escapes -E disable interpretation of backslash escapes (default) --help display this help and exit cfy

11 If -e is in effect, the following sequences are recognized:
\\ backslash \a alert (BEL) \b backspace \c produce no further output \e escape \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \0NNN byte with octal value NNN (1 to 3 digits) \xHH byte with hexadecimal value HH (1 to 2 digits) 6-9-15

12 grep, egrep, fgrep - print lines matching a pattern SYNOPSIS
GREP(1) GREP(1) NAME grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...] DESCRIPTION grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. 6-9-15

13 Divide a file into several parts (columns)
cut Divide a file into several parts (columns) Writes to standard output selected parts of each line of each input file, or standard input if no files are given or for a file name of `-'. Syntax cut [OPTION]... [FILE]... In the options below, BYTE-LIST, CHARACTER-LIST, and FIELD-LIST are one or more numbers or ranges (two numbers separated by a dash) Bytes, characters, and fields are are numbered starting at 1 and separated by commas. Incomplete ranges can be given: -M means 1-M ; N- means N through end of line or last field. 6-9-15

14 6-9-15

15 tr - translate or delete characters Synopsis
Name tr - translate or delete characters Synopsis tr [OPTION]... SET1 [SET2] Description Translate, squeeze, and/or delete characters from standard input, writing to standard output. -c, -C, --complement use the complement of SET1 -d, --delete delete characters in SET1, do not translate -s, --squeeze-repeats replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character 6-9-15

16 character with octal value NNN (1 to 3 octal digits) \\ backslash \a
audible BEL \b backspace \f form feed \n new line \r return \t horizontal tab \v vertical tab 6-9-15

17 CAT(1) User Commands CAT(1) NAME top
cat - concatenate files and print on the standard output SYNOPSIS top cat [OPTION]... [FILE]... DESCRIPTION top Concatenate FILE(s) to standard output. With no FILE, or when FILE is -, read standard input. -A, --show-all equivalent to -vET -b, --number-nonblank number nonempty output lines, overrides -n -e equivalent to -vE -E, --show-ends display $ at end of each line -n, --number number all output lines 6-9-15

18 wc - print newline, word, and byte counts for each file Synopsis
Name wc - print newline, word, and byte counts for each file Synopsis wc [OPTION]... [FILE]... wc [OPTION]... --files0-from=F Description Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts 6-9-15

19 6-9-15 Name ls - list directory contents Synopsis
ls [OPTION]... [FILE]... Description List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file -b, --escape print octal escapes for nongraphic characters --block-size=SIZE use SIZE-byte blocks. See SIZE format below -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime -C list entries by columns --color[=WHEN] colorize the output. WHEN defaults to 'always' or can be 'never' or 'auto'. More info below -d, --directory list directory entries instead of contents, and do not dereference symbolic links -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -ls --color -F, --classify append indicator (one of to entries --file-type likewise, except do not append '*' --format=WORD across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C --full-time like -l --time-style=full-iso -g like -l, but do not list owner --group-directories-first group directories before files. augment with a --sort option, but any use of --sort=none (-U) disables grouping -G, --no-group in a long listing, don't print group names -h, --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G) --si likewise, but use powers of 1000 not 1024 -H, --dereference-command-line follow symbolic links listed on the command line --dereference-command-line-symlink-to-dir follow each command line symbolic link that points to a directory --hide=PATTERN do not list implied entries matching shell PATTERN (overridden by -a or -A) --indicator-style=WORD Name ls - list directory contents Synopsis ls [OPTION]... [FILE]... Description List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort. Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. --author with -l, print the author of each file -b, --escape print octal escapes for nongraphic characters --block-size=SIZE use SIZE-byte blocks. See SIZE format below -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime -C list entries by columns --color[=WHEN] colorize the output. WHEN defaults to 'always' or can be 'never' or 'auto'. More info below -d, --directory list directory entries instead of contents, and do not dereference symbolic links -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -ls --color 6-9-15

20 shift - shift positional parameters SYNOPSIS shift [n] DESCRIPTION
NAME shift - shift positional parameters SYNOPSIS shift [n] DESCRIPTION The positional parameters shall be shifted. Positional parameter 1 shall be assigned the value of parameter (1+n), parameter 2 shall be assigned the value of parameter (2+n), and so on. The parameters represented by the numbers "$#" down to "$#-n+1" shall be unset, and the parameter '#' is updated to reflect the new number of positional parameters. The value n shall be an unsigned decimal integer less than or equal to the value of the special parameter '#' . If n is not given, it shall be assumed to be 1. If n is 0, the positional and special parameters are not changed. 6-9-15

21 pipes The format for a pipeline is:
[time [-p]] [ ! ] command [ [|│|&] command2 ... ] e.g: command1 | commnd2 cfy

22 programming in bash conditionals loops arrays functions cfy

23 File permissions chmod and ls afsconnect1-44 ~ >: chmod 751 cfy
afsconnect1-45 ~ >: ls -ld cfy drwxr-x--x 4 yurkowsk afs 2048 Apr 12 10:58 cfy/ cfy

24 Variable and special variables
$# $? $* $1 - $9 $a ${a} ${a[*]} cfy

25 Useful cc options -c suppress linking -I add directory to include list
-o renames output file -L add directory to library list -v Causes the compiler to perform more and stricter semantic cfy

26 preprocessor cpp cfy

27 linking ar ld cfy

28 manipulating pointers
linked lists binary trees stacks heaps cfy

29 client/server library
socket, bind, listen, accept and friends cfy

30 stdio.h fopen, fprintf, fread, etc. cfy

31 string library strcmp, strcat, strcpy, etc cfy

32 Bit operations & | ^ ~ >> <<

33 #include <stdio.h>
main(){ int sum=0, j; printf("input:\n"); while(scanf("%d",&j)!=EOF) sum|=j; printf("output:\n%x\n",sum); } afsconnect1-153 ~ >: mysum input: 3 5 output: 7

34

35 solution

36 cfy

37 cfy

38 Homework: server side Write a server which accepts as command line argument a port number on the local host. It should attempt open that port. If it cannot open the port it should print the value of errno and exit. Otherwise it should listen for incoming requests on that port. It should write to standard output any input it receives on the socket on which it is listening.

39 int main(int argc, char *argv[])
{ int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); /* host to network */ if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding");

40 #include <stdio.h> main(){ int j=123;
Big-endian versus little-endian: Big-endian is an order in which the "big end" (most significant value in the sequence) I s stored first (at the lowest storage address). #include <stdio.h> main(){ int j=123; printf("%.4x %.4x\n",j,htons(j)); } afsconnect1-129 ~ >: ./testhtons 007b 7b00

41 listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); bzero(buffer,256); n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s\n",buffer); n = write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); close(newsockfd); close(sockfd); return 0; }

42 Homework: client side Write a client which accepts as command line arguments the name of a server and a port on that server. The client should attempt to open a socket to that server/port. If it fails it should output the reason and errno. Otherwise it should read a line from standard input and write it to the server and it the example above.

43 int main(int argc, char *argv[])
{ int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n");

44 The <netdb.h> header defines the hostent structure that includes
at least the following members: char *h_name Official name of the host. char **h_aliases A pointer to an array of pointers to alternative host names, terminated by a null pointer. int h_addrtype Address type. int h_length The length, in bytes, of the address. char **h_addr_list A pointer to an array of pointers to network addresses (in network byte order) for the host terminated by a null pointer.

45 bzero((char *) &serv_addr,
sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); printf("Please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); if (n < 0) error("ERROR writing to socket"); n = read(sockfd,buffer,255); error("ERROR reading from socket"); printf("%s\n",buffer); close(sockfd); return 0; }

46 Bit operator exercise Write a mysum program which read integers from standard input, exclusive ors them together and writes the resulting value in hex to standard output. Hint use scanf to read the integers.

47

48 pointers char c='a', d='b', *cp; cp= &d; *cp=c; cp++;

49 argc, argv main(int argc, char *argv[])

50 Preprocessor directives
#include < > versus double quotes #define #ifdef #else #endif

51 break

52 python the version of python on afs5 requires parenthesis with print

53 conditional operators
same as C with the addition of <> tmp]$ python Python (default, Aug , 15:58:25) [GCC (Red Hat )] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> x=2 >>> print x == 2 True >>> print x != 2 False >>> print x <> 2 >>> cfy

54 booleans and & or instead of && and || >>> x=1
>>> y=2 >>> if x == 1 and y == 2: print ("both true") ... both true >>> cfy

55 similarly not instead of !
>>> print (not False) True >>> cfy

56 >>> if x == 2 and y == 2:. print ("both true"). else:
>>> if x == 2 and y == 2: ... print ("both true") ... else: ... print ("at least one is false") ... at least one is false >>> cfy

57 Indentation cfy

58 numeric type floats and integers: cfy

59 strings single or double quotes cfy

60 comments # this is a comment cfy

61 arithmetic operators + - * % / // (floor) ** (power) e=2.71 pi=3.14
print ( e ** pi) cfy

62 arrays afsconnect1-51 python >: cat array.py array = [1,2,3]
print (array[0]) print (array[1]) print (array[2]) afsconnect1-52 python >: python array.py 1 2 4 cfy

63 lists afsconnect1-59 python >: cat list.py list = [] list.append(1) list.append(2) list.append(3) # prints out 1,2,3 for x in list: print (x) afsconnect1-60 python >: python list.py afsconnect1-61 python >: cfy

64 can use multiplication operator on lists
>>> print ([1,2,3] * 3 ) [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> cfy

65 C style format strings >>> name="Lou Cifer" >>> grade=66.6 >>> print ("the grade for %s is %f " % (name , grade ) ) the grade for Lou Cifer is >>> print (len(name)) 9 >>> print (name.index("C")) 4 >>> cfy

66 import other modules $ cat main.py import sys print (len(sys.argv)) $ python main.py 1 $ python main.py a bc def 4 $ cfy

67 loops import sys y=0 x=len(sys.argv) while y < x: print sys.argv[y] y += 1 $ python main.py a bc def main.py a bc def $ cfy

68 atoi (int) $ cat convert.py string = "1" integer = 1 print ("string and integer are not the same:"), print ( integer == string ) print ("unless converted:"), print ( integer == int(string)) $ python convert.py string and integer are not the same: False unless converted: True $ cfy

69 reprise of homework 4, problem 3 redo with python instead of C or bash
cfy

70 something like this: cfy

71 afsconnect1-55 python >: cat sort
afsconnect1-55 python >: cat sort.py # print sort and print integers in ascending order import sys array=sys.argv x=len(sys.argv) if x < 2: print ("Usage: python %s sting of integers " % sys.argv[0]) sys.exit(-1) y=1 while y < x: z=y while z < x: if int(array[y]) > int(array[z]): temp=array[y] array[y]=array[z] array[z]=temp z+=1 y+=1 z=1 while z < x : print ( array[z], end=" " ) print ("\n") afsconnect1-56 python >: cfy


Download ppt "Cs288 Intensive Programming in Linux"

Similar presentations


Ads by Google