We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Published bySelena Berner
Modified over 4 years ago
CMPT 471 Networking II Linux Primer 1© Janice Regan, 2013
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
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
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
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
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
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
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
Root file structure © Janice Regan, 2013 9
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
1 Introduction to UNIX Ke Liu
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
©Colin Jamison 2004 Introduction to Linux Colin Jamison.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Preparation for working in the CSIL Basic LINUX operations.
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.
Linux+ Guide to Linux Certification, Second Edition
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
The Unix Shell. Operating System shell The shell is a command interpreter It forms the interface between a user and the operating system When you log.
Unix Basics. Systems Programming: Unix Basics 2 Unix Basics Unix directories Important Unix file commands File and Directory Access Rights through.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Linux Commands LINUX COMMANDS.
L INUX C OMMAND L INE I NTERFACE G UNAANBAN.G
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Learning basic Unix command IT 325 operating system.
COMP1070/2002/lec4/H.Melikian COMP1070 Lecture #5 Files and directories in UNIX Various types of files File attributes Notion of pathname Commands.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
Introduction to Unix Bent Thomsen Institut for Datalogi Aalborg Universitet.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
1 Lecture 2 Working with Files and Directories COP 3344 Introduction to UNIX.
LIN Unix Lecture 3 Hana Filip. LIN UNIX Resources UNIX Tutorials UNIX help for.
© 2020 SlidePlayer.com Inc. All rights reserved.