Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Similar documents
Useful Unix Commands Cheat Sheet

Linux shell programming for Raspberry Pi Users - 2

Basic Linux (Bash) Commands

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

A shell can be used in one of two ways:

Assignment clarifications

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

LING 408/508: Computational Techniques for Linguists. Lecture 5

Bourne Shell Reference

Shells and Shell Programming

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

How to print or access value of UDV (User defined variables)

LOG ON TO LINUX AND LOG OFF

Introduction: What is Unix?

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Shell Start-up and Configuration Files

Part 1: Basic Commands/U3li3es

Shells and Shell Programming

Using Sapelo2 Cluster at the GACRC

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Linux Bash Shell Scripting

GACRC User Training: Migrating from Zcluster to Sapelo

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Title:[ Variables Comparison Operators If Else Statements ]

UNIX System Programming Lecture 3: BASH Programming

Migrating from Zcluster to Sapelo

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Mills HPC Tutorial Series. Linux Basics II

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

CSC 2500: Unix Lab Fall 2016

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Review of Fundamentals

UNIX shell scripting

Lab 2: Linux/Unix shell

Perl and R Scripting for Biologists

Introduction to HPC Using the New Cluster at GACRC

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

COMP 4/6262: Programming UNIX

Bash scripting basics

Shell programming. Introduction to Operating Systems

Introduction to High Performance Computing (HPC) Resources at GACRC

Shell Programming (bash)

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

The Unix Shell. Pipes and Filters

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

CSE 15L Winter Midterm :) Review

Shells & Shell Programming (Part B)

Introduction to Linux Basics Part I. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Chapter 9. Shell and Kernel

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Chapter 4. Unix Tutorial. Unix Shell

9.2 Linux Essentials Exam Objectives

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Bash shell programming Part II Control statements

Processes and Shells

Vi & Shell Scripting

Command-line interpreters

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Bash Programming. Student Workbook

Introduction To. Barry Grant

Conditional Control Structures. Dr.T.Logeswari

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Bourne Shell Programming Topics Covered

Topic 2: More Shell Skills

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

Shell Programming (Part 2)

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College

Програмиранев UNIX среда

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Common File System Commands

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

bash Execution Control COMP2101 Winter 2019

Computer Systems and Architecture

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Operating Systems. Copyleft 2005, Binnur Kurt

Transcription:

Introduction to Linux Basics Part II 1 Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala pakala@uga.edu

2 Variables in Shell HOW DOES LINUX WORK? Shell Arithmetic I/O and Redirection Redirecting output, more, less, cat Piping, Sorting, Pattern Matching, Searching Decision making If condition Loops For loop While loop

3 Variables in Shell In Linux (Shell), there are two types of variable: System variables: Created and maintained by Linux itself, this type of variable defined in CAPITAL LETTERS. User defined variables (UDV) : Created and maintained by user, this type of variable defined in lower letters. BASH=/bin/bash System Variable shell name Meaning BASH_VERSION=1.14.7(1) COLUMNS=80 HOME=/home/pakala OSTYPE=Linux PATH=/usr/bin:/sbin:/bin:/usr/sbin PWD=/home/students/Common SHELL=/bin/bash USERNAME=pakala shell version name No. of columns for our screen home directory Operating System type path settings current working directory shell name User name who is currently login to this PC

4 How to define User defined variables (UDV) Syntax: variable name=value 'value' is assigned to given 'variable name' Value must be on right side = sign Examples: $ no=10 # this is fine $ 10=no # this is NOT Fine # value must be on right side of = sign $ n=10 #to define variable n having value 10 $ vech=bus #to define variable vech having value Bus

5 Rules for Naming variable name Don't put spaces on either side of the equal sign when assigning value to variable Example: the following variable declaration there will be no error $ no=10 # No error But there will be problem for any of the following variable declaration: $ no =10 $ no= 10 $ no = 10 Variables are case-sensitive $ no=10 #will print 10 $ No=11 #will print 11 $ NO=20 #to print value 20,we need to use $echo $NO You can define NULL variable $ tech= $ tech= #nothing will be shown as variable has no value Do not use?,* etc, to name your variable names

6 echo Command echo command is used to display text or value of variable. echo [options] [string, variables...] Options: -n Do not output the trailing new line. -e Enable interpretation of the following backslash escaped characters in the strings: \a alert (bell) \b backspace \c suppress trailing new line \n new line \r carriage return \t horizontal tab \\ backslash $ echo e An apple a day keeps away \a\tdoctor\n

7 How to print or access value of UDV (User defined variables) To print or access UDV: Syntax: $variablename $ fruit=mango $ n=25 $ echo $fruit $ echo $n Shell Arithmetic To perform arithmetic operations. Syntax: expr op1 math-operator op2 $ expr 3 + 3 $ expr 5-1 $ expr 20 / 2 $ expr 20 % 3 $ expr 3 \* 2 $ echo `expr 7 + 3`

8 More about Quotes There are three types of quotes: Quotes Name Meaning " Double Quotes ' Single quotes "Double Quotes" - Anything enclosed in double quotes removed meaning of that characters (except \ and $). 'Single quotes' - Enclosed in single quotes remains unchanged. ` Back quote `Back quote` - To execute command $ echo Today is date #cannot print message with today s date $ echo Today is `date` # will print today s date

9 Quoting Examples $ FRUIT=apples $ echo I like $FRUIT # $ is disabled by $ I like $FRUIT $ echo I like $FRUIT # $ is not disabled by $ I like apples $ echo I like \$FRUIT # $ is disabled forcedly by preceding \ $ I like $FRUIT $ echo `pwd` # ` is disabled by $ `pwd` $ echo `pwd` # ` is not disabled by $ /home/gacrc-instruction/pakala

10 The read Statement Syntax: read variable1, variable2,...variablen $ nano hello.sh #!/bin/bash # script to read your name from keyboard # echo please enter your name: read name echo Hello $name, Lets be friends! $ chmod 744 hello.sh $./hello.sh $ please enter your name:suchitra $ Hello suchitra, Lets be friends!

11 Wild cards Wild card Meaning Examples *? [...] Matches any string or group of characters. Matches any single character. Matches any one of the enclosed characters $ ls * Lists all files $ ls a* Lists all files whose first name is starting with letter 'a' $ ls *.c Lists all files having extension.c $ ls ut*.c $ ls? $ ls fo? Lists files having extension.c but file name must begin with 'ut'. Lists all files whose names are 1 character long Lists all files whose names are 3 character long and file name begin with fo $ ls [abc]* Lists all files beginning with letters a,b,c

12 I/O AND REDIRECTION Programs and commands can contain both inputs and outputs Input and outputs of a program are called "streams " in Linux There are three types of streams STDIN: "standard input"-- by default, input from the keyboard STDOUT: "standard output"--by default, output sent to the screen STDERR: "standard error"--by default, error output sent to the screen Output Redirection To redirect all directory content to output_file: $ ls > my_file Redirection of this sort will create the named file if it doesn't exist, or else overwrite the named file if it does exist already. You can append the output file instead of rewriting it using a double ">>" $ ls >> my_file

13 I/O AND REDIRECTION Input Redirection Input can also be given to a command from a file instead of typing it to the screen like this: $ samplefile < file1 Error Redirection When performing normal redirection, STDERR will not be redirected Many bash programmers find it useful to redirect only STDERR to a separate file If the program produces a lot of output, to make it easier to find the errors which are thrown from your program. Using the bash shell, this can be accomplished with "2>" $ samplefile 2> error_file In addition one may merge STDERR to STDOUT with 2>&1 $ samplefile > output_file 2>&1

14 Redirecting output, cat, more, less list command and > to redirect your output to a file named mylist $ ls -l /etc > mylist There are three methods for viewing a file from the command prompt: cat, more and less cat shows the contents of the entire file at the terminal, and scrolls automatically $ cat mylist more shows the contents of the file, pausing when it fills the screen. Use the spacebar to advance one page at a time $ more mylist less also shows the contents of the file, pausing when it fills the screen. Use the spacebar to advance one page at a time, or use the arrow keys to scroll one line at a time (q to quit). "g" and "G" will take you to the beginning and end, respectively $ less mylist

15 Piping A pipe is a way to connect the output of one program to the input of another program without any temporary file Using the pipe operator " " you can link commands together. The pipe will link the standard output from one command to the standard input of another Syntax: command1 command2 $ ls more #output of ls command is given as input to more command $ who sort #output of who command is given as input to sort command which will print sorted list of user s $ who sort > user_list # out of sort is redirected to user_list file $ who wc l #prints number of users who logon to system $ who grep suchi #print if particular user name, if logon or nothing is printed

16 Sorting The Linux sort command sorts the content of a file or any STDIN, and prints the sorted list to the screen $ cat temp.txt cherry apple x-ray clock orange bananna $ sort temp.txt apple bananna cherry clock orange x-ray To see sorted list in reverse order, use the -r option $ sort -r temp.txt x-ray orange clock cherry bananna apple

17 Pattern Matching grep is another useful search utility It searches the named input file for lines that match the given pattern and prints those matching lines In the following example, search for instances of the word World" in the file sample1 If there are no matches, grep will not print anything to the screen $ cat sample1 Welcome to the Linux World. Linux is free and open source Software. $ grep World sample1 Welcome to the Linux World.

18 Searching Finding files on the system and finding a particular text string within a file are very useful. searching in /usr/lib, looking for files named libmenu.so, and whenever it finds one, prints its full path The find command is useful for finding where missing libraries are located, so the path may be added to the LD_LIBRARY_PATH environment variable $ find /usr/lib -name libmenu.so -print grep command searches for patterns and prints matching lines Here, it looks for "score" in the file lincoln.txt $ grep score lincoln.txt In following example, grep searches input from ps -ef (which outputs all processes in full format), and prints out a list of csh users $ ps -ef grep bash

19 More commands on one command line: Syntax:command1;command2 $ pwd ; ls $ cd.. ; ls $ date ; who Tilde Expansion (Home Expansion): ~ $ cd ~username # home directory associated username $ cd ~ # replaced by $HOME $ cd ~/ # same as above Command Substitution: `command` (` is back quota!) $ cd `pwd` # same as cd /home/gacrc-instruction/pakala

20 Decision Making bc - Linux calculator program. Expression Meaning to us Your Answer BC's Response 5 > 12 Is 5 greater than 12 NO 0 5 == 10 Is 5 is equal to 10 NO 0 5!= 2 Is 5 is NOT equal to 2 YES 1 5 == 5 Is 5 is equal to 5 YES 1 1 < 2 Is 1 is less than 2 Yes 1 In bc, relational expression always returns true (1) or false (0 - zero).

21 if condition if condition which is used for decision making in shell script If given condition is true then command1 is executed. Syntax: if condition then command1 if condition is true or if exit status of condition is 0(zero) fi #!/bin/bash # #Script to print file # if cat $1 then echo -e "\nfile $1, found and successfully echoed" fi $ nano sampledata.sh $ chmod 744 sampledata.sh $./sampledata.sh sample Hello!!!!! Welcome to Linux world... File sample, found and successfully echoed Shell script name is sampledata.sh($0) sample (which is $1) is a file If sample file exists, it will print sample files content to the screen.

22 Test Command test command or [ expr ] is used to see if an expression is true, and if it is true it returns zero(0), otherwise returns nonzero for false. Syntax: test expression OR [ expression ] #!/bin/bash # # Script to see whether argument is positive # if test $1 -gt 0 then echo "$1 number is positive" fi $ chmod 744 test.sh $./test.sh 25 25 number is positive $./test.sh -5 Nothing is printed

23 File testing String testing ARITH testing Logical testing Flow Control Test Expression Description -e file True if file exists -d or -f file True if file exists and is a directory or a regular file -r or -w or -x file True if file exists and is readable or writable or executable -s file True if file exists and has a nonzero size file1 -nt or -ot file2 True if file1 is newer or older than file2 -z or -n string True if the length of string is zero or nonzero str1 == str2 str1!= str2 True if the strings are equal True if the strings are not equal arg1 OP arg2 OP is one of eq, -ne, -lt, -le, -gt, or -ge. Arg1 and arg2 may be +/- integers! expr True if expr is false expr1 -a expr2 expr1 -o expr2 True if both expr1 AND expr2 are true True if either expr1 OR expr2 is true

24 Loops for Loop: Syntax: for { variable name } in { list } do execute one for each item in the list until the list is finished done Example: for i in 1 2 3 4 5 do echo "Welcome $i times" done $ chmod 744 forloop.sh $./forloop.sh welcome 1 times welcome 2 times welcome 3 times welcome 4 times welcome 5 times

25 While Loop: Syntax: while [ condition ] do command1 command2 -- done $ chmod 744 whileloop.sh $./whileloop.sh 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 #!/bin/bash #Script to test while statement if [ $# -eq 0 ] then echo "Error - Number missing from command line argument echo syntax : $0 number echo " Use to print multiplication table for given number" exit 1 fi n=$1 i=1 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1` done

26 Bash Profile Why we have those automatically set shell variables? Configure your working environment on Linux as you wish! Example:.bash_profile for interactive login shell if [ -f ~/.bashrc ]; then # if.bashrc exists and is a regular file, then. ~/.bashrc # run/source it in current shell to fi # make interactive login and non-login shell # to have the same environment # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH

27 Shell Scripting Examples: #!/bin/bash # if no vehicle name is given # i.e. -z $1 is defined and it is NULL # if no command line argument if [ -z $1 ] then rental="*** Unknown vehicle ***" elif [ -n $1 ] then # otherwise make first argument as rental rental=$1 fi case $rental in "car") echo "For $rental $45 per day ;; "van") echo "For $rental $85 per day";; "jeep") echo "For $rental $55 per day";; *) echo "Sorry, I can not get a $rental for you";; esac

28 Shell Scripting Examples Serial job script sub.sh running NCBI Blast + (Sapelo) #PBS -S /bin/bash #PBS -q batch #PBS -N testblast #PBS -l nodes=1:ppn=1:amd #PBS -l mem=20gb #PBS -l walltime=48:00:00 Linux shell (bash) Queue name (batch) Name of the job (testblast) Number of nodes (1), number of cores/node (1), node type (AMD) Maximum amount of physical memory (20 GB) used by the job Maximum wall clock time (48 hours) for the job, default 6 minutes cd $PBS_O_WORKDIR Use the directory from which the job is submitted as the working directory module load ncbiblast+/2.2.29 Load the module of ncbiblast+, version 2.2.29 time blastn [options] > outputfile 2>&1 Run blastn with time command to measure the amount of time it takes to run the application https://wiki.gacrc.uga.edu/wiki/running_jobs_on_sapelo

29 Shell Scripting Examples Threaded job script sub.sh running NCBI Blast+ with 24 threads(sapelo) #PBS -S /bin/bash #PBS -q batch #PBS -N testblast #PBS -l nodes=1:ppn=24:amd Number of nodes (1), number of cores/node (24), node type (AMD) #PBS -l mem=50gb Number of threads (24) = Number of cores requested (24) #PBS -l walltime=480:00:00 cd $PBS_O_WORKDIR #PBS -M jsmith@uga.edu #PBS -m ae #PBS -j oe Email address to receive a notification for computing resources Send email notification when job aborts (a) or terminates (e) Standard error file (testblast.e1234) will be merged into standard out file (testblast.o1234) module load ncbiblast+/2.2.29 time blastn -num_threads 24 [options] > outputfile 2>&1 Run blastn with 24 threads (-num_threads 24) https://wiki.gacrc.uga.edu/wiki/running_jobs_on_sapelo

30 Linux Command Reference ls > directory listing cd > change directory pwd > show current directory mkdir dir > create a directory dir rm file > delete file cp file1 file2 > copy file1 to file2 mv file1 file2 > rename or move file1 to file2 ln -s file link > create symbolic link link to file touch file > create or update file cat > file > places standard input into file more file > output the contents of file head file > output the first 10 lines of file tail file > output the last 10 lines of file file > to determine a file's type grep pattern files > search for pattern in files ps > display your currently active processes top > display all running processes kill pid > kill process id pid chmod > change the permissions of file 4 read (r) 2 write (w) 1 execute (x)

31 Thank You