Download presentation
Presentation is loading. Please wait.
Published bySelena Berner Modified over 9 years ago
1
CMPT 471 Networking II Linux Primer 1© Janice Regan, 2013
2
Shell Scripts A shell script is an executable file containing a series of shell commands. When the shell script is executed the commands in the file are executed in sequence by the shell. A shell script records a series of command line actions and easily executes them again and again Assures repeatability of experiments and processes Allows long series of commands to be easily tested and debugged There are different shell scripting languages such as csh (C shell), tcsh sh (Bourne shell), bash (Bourne again shell) Perl …… © Janice Regan, 2013 2
3
Selecting a Shell Script Language On the command line type the name of the shell followed by return to enter the shell. exit followed by return to leave the shell. To determine which default shell you are presently using echo $SHELL finger youruserid (your default shell) The present shell will be indicated by the prompt To indicate which shell to use inside a shell script in the first line of your script file#! /bin/csh To indicate which shell to use when executing a shell script called scriptname bash scriptname arguments or csh scriptname arguments © Janice Regan, 2013 3
4
Executing a Shell Script Your script file must be executable, you will need to change the permissions to make it executable (discuss how later in this lecture) Executing a shell script called scriptname bash scriptname arguments csh scriptname arguments source scriptname arguments ./scriptname arguments © Janice Regan, 2013 4
5
Using Basic Unix Commands Any basic Unix command can be run from the command line, or from within a shell script To work with shell scripts you must first be familiar with basic Unix commands Commands are used to move around within the file system, to create and operate on files, to interact with the operating system © Janice Regan, 2013 5
6
Some basic commands Remember UNIX is case sensitive yppasswd username (username is optional) Executed from your command line Will ask you to input your old password and your new passwd twice You can only change your own passwd unless you are root man commandname or info commandname Tell me how to use the command with name commandname su Become the root user (superuser, substitute user) You will need to be root to complete many of your assignments. Do so with care.. whoami Tell me my username © Janice Regan, 2013 6
7
Directories Directory creation, navigation, removal mkdir directorynamemake directory directoryname rmdir directorynameremove empty directory cd directorypathgo to directory directorypath ls directorypathlist files in directory directorypath cdreturn to your home directory lslist files in this directory pwdshow path to and name of current directory cd..Move to the parent directory of this directory (one layer further up the directory tree © Janice Regan, 2013 7
8
Directory structure Dir1 Dir5Dir6Dir7 © Janice Regan, 2013 8 In myhome cd Dir2 (goes to Dir2 ) cd Dir2/Dir5 (goes to Dir5 ) In Dir5 cd.. (goes to Dir3) cd../.. Or cd (goes to myhome) cd../Dir6 (goes to Dir6) Anywhere cd will take you to myhome (your home directory) cd / takes you to the root directory myhome Dir2Dir3Dir4
9
Root file structure © Janice Regan, 2013 9
10
Files To make a file open it using your favorite text editor mv filepath1 filepath2 Moves (renames) a file or directory mv a b file previously named a is now named b mv a../a file a is moved from the present directory to the parent directory of the present directory rm filepathremoves (deletes) a file rm a deletes file a from the current directory rm../a removes file a from the parent of the present directory grep pattern filepaths Find all occurrences of pattern in requested files © Janice Regan, 2013 10
11
Files more filename or less filename Displays contents of file filename one page (screen) at a time cp filename1 filename2 Make a copy of filename1 with name filename2 cat filename1 filename2 filename3 Will print the contents of each file in sequence Contents of filename1 Contents of filename2 Contents of filename3 © Janice Regan, 2013 11
12
Wildcards Wildcards are used to represent multiple possibilities * matches any number of characters ? Matches a single character Examples ls a? List all files in the current directory beginning with a and having a name of length 2 characters. grep mystring */*file Find all occurences of the string “mystring” in all files whose names end with the string “file”. The files ending with string “file” must be in a subdirectory of the present directory rm [a-c]* Remove all files with filenames beginning with a b or c from the current directory © Janice Regan, 2013 12
13
Redirection Redirect input and/or output > filename redirect standard output from screen to file. cat file1 file2 file3 > f4 A new file f4 is opened and the contents of file1, file2 and file 3 are successively added to the file f4. If f4 exists it will be overwritten. < filename take input from file filename a.out < datafile (a.out is the default output file for a compiled executable) >> filename redirect standard output from screen to file. Appends output to an existing file cat file1 file2 file3 >> f4 The contents of file1 then file2 then file3 will be successively appended to the current contents of f4 © Janice Regan, 2013 13
14
Piping Piping allows you to send the output of one process to become the input of another process without using io to store it in an intermediate file ls a* | more List the files in the current directory whose names start with a, one page at a time © Janice Regan, 2013 14
15
File Permissions Can use ls –l to find present permission for files drwxrwxrwxfor directory -rwxrwxrwxfor file u g o 3 sets of permissions User (u) group (g) other(o) Each set allows or disallows read write & or execute rwx (allow all) rw- (read write allowed) r - - (read only) Order of sets is ugo © Janice Regan, 2013 15
16
Setting File Permissions: 1 Permissions can be reset by using chmod chmod can specify permission in two ways Numeric values read(4) write(2) execute(1). Sum desired values to give one digit permission value for each set Read write and execute 7, Write 2, Read and Execute 5 Adding or removing particular permissions Add execution permissions for user chmod u+x filename Remove write permissions for other chmod o+w filename Add read permissions for everyone chmod a+r filename © Janice Regan, 2013 16
17
File Permissions Examples chmod u-x filename (remove owners execute permission) chmod a+w filename (make the file readable for all users) chmod 644 filename (make the file readable for all users but writeable only by the owner) chown newowner filename (you must own the file or be root) chgroup newgroup filename © Janice Regan, 2013 17
18
Processes Command & Run command as a background process cntrlZ Suspend (temporarily stop) foreground process bg Move suspended process to background fg Move background process or suspended process to the foreground cntrlC Terminate the foreground process © Janice Regan, 2013 18
19
Processes ps or ps -l List information about all current processes kill pidnumber Kill background process with PID pidnumber (use ps to find the pidnumber of the process) ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 8 S 37636 7058 7054 0 46 20 ? 273 ? pts/38 0:01 csh 8 S 37636 7072 7058 0 51 20 ? 296 ? pts/38 0:00 bash To assure a process will die use kill -9 pidnumber © Janice Regan, 2013 19
20
Checking shared memory check after your process terminates jregan@cs-nx-01:~$ ipcs ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x00000000 1885601818 jregan 600 393216 2 dest ------ Semaphore Arrays -------- key semid owner perms nsems 0x00000000 5373963 dwlouie 666 25 ------ Message Queues -------- key msqid owner perms used-bytes messages © Janice Regan, 2013 20
21
When shared memory is left When you still have entries in the shared memory section Look at the value in the nattach (number of attached processes) if it is 0 then you can remove the memory segments using the provided script cleanSem © Janice Regan, 2013 21
22
Using the cleanSem script Copy the script from the class website Change the mode of the file you now have in your LINUX directory so it can be executed Chmod +x cleanSem Run the script twice. To run ./cleanSem © Janice Regan, 2013 22
23
When shared memory is left When you still have entries in the shared memory section Look at the value in the nattach (number of attached processes) if it is not 0 then the memory segments are still connected to at least one process You need to find and remove that process © Janice Regan, 2013 23
24
Removing processes (300) Find the processes that are connected to your shared memory If your userid is myuserid ps ux | grep myuserid | grep fork Look at the resulting list of processes, finding one that did not start at the time you logged in. Kill that process (kill -9 processid) © Janice Regan, 2013 24
25
Removing other processes Find the processes that are part of your last run. If your userid is myuserid and you ran executable./myprog ps ux | grep myprog Kill the processes that include./myprog © Janice Regan, 2013 25
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.