250 likes | 352 Views
CMPT 471 Networking II. Linux Primer . 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.
E N D
CMPT 471Networking II Linux Primer
Shell Scripts • Ashell 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 ……
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 argumentsor csh scriptname arguments
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
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
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
Directories • Directory creation, navigation, removal • mkdir directoryname make directory directoryname • rmdir directoryname remove empty directory • cd directorypath go to directory directorypath • ls directorypath list files in directory directorypath • cd return to your home directory • ls list files in this directory • pwd show path to and name of current directory • cd .. Move to the parent directory of this directory (one layer further up the directory tree
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 Dir1 Dir2 Dir3 Dir4 Dir5 Dir6 Dir7 Directory structure
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 filepath removes (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
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
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
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
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
File Permissions • Can usels –l to find present permission for files • drwxrwxrwx for directory • -rwxrwxrwx for 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
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
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
Processes • Command & • Run command as a background process • cntrlZ • Suspend (temporarily stop) foreground process • bgMove suspended process to background • fg Move background process or suspended process to the foreground • cntrlC • Terminate the foreground process
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
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
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
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
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
Removing processes (300) • Find the processes that are connected to your shared memory • If your userid is myuserid psux | grepmyuserid | grepfork • 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)
Removing other processes • Find the processes that are part of your last run. • If your userid is myuserid and you ran executable ./myprog psux | grepmyprog • Kill the processes that include ./myprog