UNIX Shell-Scripting Basics - Miralishahidi.ir

1y ago
11 Views
2 Downloads
9.22 MB
50 Pages
Last View : 21d ago
Last Download : 3m ago
Upload by : Fiona Harless
Transcription

UNIX Shell-Scripting With focus on bash BINP14 Björn Canbäck

Outline What is a shell? A shell script? Introduction to bash Running Commands BINP14 Björn Canbäck

What is a shell? A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one or more such commands. Source: http://en.wikipedia.org/wiki/Unix shell BINP14 Björn Canbäck

What is a shell? Input (STDIN) shell output (STDOUT) error (STDERR)

Common Shells Bash (/bin/bash) Bourne again shell C Shell (/bin/csh) Turbo C Shell (/bin/tcsh) Korn Shell (/bin/ksh) BINP14 Björn Canbäck

What is bin ? /bin /usr/bin /usr/local/bin /home/bjorn/bin BINP14 Björn Canbäck

What is a shell script? A text file With instructions Executable BINP14 Björn Canbäck

What is a Shell Script? % cat hello.sh HERE #!/bin/sh echo 'Hello world!' HERE % chmod x hello.sh % ./hello.sh Hello world!

What is a Shell Script? A Text File % cat hello.sh HERE #!/bin/sh echo 'Hello world!' HERE % chmod x hello.sh % ./hello.sh Hello world! BINP14 Björn Canbäck

What is a Shell Script? How To Run % cat hello.sh HERE #!/bin/sh echo 'Hello world!' HERE % chmod x hello.sh % ./hello.sh Hello world! BINP14 Björn Canbäck

What is a Shell Script? What To Do % cat hello.sh HERE #!/bin/sh echo 'Hello world!' HERE % chmod x hello.sh % ./hello.sh Hello world! BINP14 Björn Canbäck

What is a Shell Script? Executable % cat hello.sh HERE #!/bin/sh echo 'Hello world!' HERE % chmod x hello.sh % ./hello.sh Hello world! BINP14 Björn Canbäck

What is a Shell Script? Running it % cat hello.sh HERE #!/bin/sh echo 'Hello world' HERE % chmod x hello.sh % ./hello.sh Hello world! BINP14 Björn Canbäck

Finding the program: PATH % ./hello.sh % echo PATH /bin:/usr/bin:/usr/local/bin: /home/bjorn/bin % which echo /usr/bin/echo BINP14 Björn Canbäck

Variables and the environment % hello.sh bash: hello.sh: Command not found % PATH “ PATH:.” % hello.sh Hello, world BINP14 Björn Canbäck

Redirection echo hej test.txt echo “ hej” test.txt Expert users only: input 0 Expert users only: program cat test.txt cat INPUT Some input INPUT output 1 test.sh 2 myError text.sh myErrorAndOut 2 &1 BINP14 Björn Canbäck error 2

Quoting % echo USER % echo bjorn % echo bjorn % echo ” % echo ' USER' “ USER” USER \” \ BINP14 Björn Canbäck

How to learn man man bash man cat man man Learning the Bash Shell, 2nd Ed. “Bash Reference” Cards

Continuing lines: \ % echo This \ Is \ A \ Very \ Long \ Command Line This Is A Very Long Command Line % BINP14 Björn Canbäck

Make Your Life Easier TAB completion Control R Control S

Pipes Lots of Little Tools INPUT 0 echo echo “Hello” \ wc -c OUTPUT 1 ERROR 2 A Pipe! INPUT 0 wc OUTPUT 1 ERROR 2

Following is only if you want to learn more

Exit status (expert users) ? 0 is True % ls /does/not/exist % echo ? 1 % echo ? 0 BINP14 Björn Canbäck

Exit status: (expert users) % cat test.sh TEST exit 3 TEST % chmod x test.sh % ./test.sh % echo ? 3

Logic: test (expert users) % test 1 -lt 10 % echo ? 0 % test 1 10 % echo ? 1

Logic: test (expert users) test [ ] [[ ]] [ 1 –lt 10 ] [[ “this string” “this” ]] (( )) (( 1 10 ))

Logic: test (expert users) [ [ [ [ -f /etc/passwd ] ! –f /etc/passwd ] -f /etc/passwd –a –f /etc/shadow ] -f /etc/passwd –o –f /etc/shadow ]

An aside: (( )) for Math (expert users) % echo (( 1 2 )) 3 % echo (( 2 * 3 )) 6 % echo (( 1 / 3 )) 0

Logic: if (expert users) if something then : # “elif” a contraction of “else if”: elif something-else then : else then : fi

Logic: if (expert users) if [ USER –eq “borwicjh” ] then : # “elif” a contraction of “else if”: elif ls /etc/oratab then : else then : fi

Logic: if (expert users) # see if a file exists if [ -e /etc/passwd ] then echo “/etc/passwd exists” else echo “/etc/passwd not found!” fi

Logic: for (expert users) for i in 1 2 3 do echo i done

Logic: for (expert users) for i in /* do echo “Listing i:” ls -l i read done

Logic: for (expert users) for i in /* do echo “Listing i:” ls -l i read done

Logic: for (expert users) for i in /* do echo “Listing i:” ls -l i read done

Logic: C-style for (expert users) for (( expr1 ; expr2 ; expr3 )) do list done

Logic: C-style for (expert users) LIMIT 10 for (( a 1 ; a LIMIT ; a do echo –n “ a ” done ))

Logic: while while something do : done

Logic: while a 0; LIMIT 10 while [ " a" -lt " LIMIT" ] do echo -n " a ” a (( a 1 )) done

Counters COUNTER 0 while [ -e “ FILE.COUNTER” ] do COUNTER (( COUNTER 1)) done Note: race condition

Reusing Code: “Sourcing” % cat /path/to/my/passwords PW FTP USER “sct” PW % echo FTP USER % . /path/to/my/passwords % echo FTP USER sct %

Variable Manipulation % FILEPATH /path/to/my/output.lis % echo FILEPATH /path/to/my/output.lis % echo {FILEPATH%.lis} /path/to/my/output % echo {FILEPATH#*/} path/to/my/output.lis % echo {FILEPATH##*/} output.lis

Running Programs

Reasons for Running Programs Check Return Code Get Job Output ? OUTPUT echo “Hello” OUTPUT (echo “Hello”) Send Output Somewhere Redirection: , Pipes

Email Notification % echo “Message” \ mail –s “Here’s your message” \ borwicjh@wfu.edu

Dates % DATESTRING date %Y%m%d % echo DATESTRING 20060125 % man date

FTP the Hard Way ftp –n –u server.wfu.edu FTP user username password put FILE FTP

FTP with wget wget \ ftp://user:pass@server.wfu.edu/file wget –r \ ftp://user:pass@server.wfu.edu/dir/

FTP with curl curl –T upload-file \ -u username:password \ ftp://server.wfu.edu/dir/file

Searching: find % find /home/borwicjh \ -name ‘*.lis’ [all files matching *.lis] % find /home/borwicjh \ -mtime -1 –name ‘*.lis’ [*.lis, if modified within 24h] % man find

What is a shell? BINP14 Björn Canbäck A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to

Related Documents:

Shell Donax TU Shell Spirax S6 ATF UM Shell Donax TV Shell Spirax S6 ATF VM Shell Donax TX Shell Spirax S4 ATF HDX* Shell ATF XTR Shell Donax TA Shell Spirax S2 ATF D2 Shell ATF IID GREASES Shell Retinax CSZ Shell Gadus S4 V45AC Shell Albida HDX Shell Gadus S3 V460D Shell Retinax LX2 Shell

Shell, Unix lesystem, basic tools Combining tools/commands (pipe'ing) Advanced tools Regular expressions Stream manipulation Scripting Shell scripting Python scripting Instructor: Bruno Abrahao CS2043 - Unix Tools & Scripting. What are scripts? Programs written for a special run-time environment that can

ŁVery advanced shell scripting Œ try these courses instead:! fiUnix Systems: Shell Scripting (III) fl! fiProgramming: Python for Absolute Beginners fl bashis probably the most common shell on modern Unix/Linux systems Œ in fact, on most modern Linux distributions it will be the default shell (the shell users get if they

What is a Shell Script or shell scripting 13 Why shell scripting 14 Chapter 1 Challenges 16 Chapter 2: Getting Started With Shell Programming 17 The bash shell 17 Shell commands 19 The role of shells in the Linux environment 21 Other standard shells 23 Hello, World! Tutorial 25 Shebang 27 Shell Comments 29 Setting up permissions on a script 30

Bash Shell The shell of Linux Linux has a variety of different shells: – Bourne shell (sh), C shell (csh), Korn shell (ksh), TC shell (tcsh), Bour ne Again shell (bash). Certainly the most popular shell is “bash”. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C

Bash shell scripting tutorial Scott T. Milner September 9, 2020 1 Introduction The shell is the program we interact with when we type at a Unix command line prompt. There are actually several di erent Unix shell programs; the most commonly used is bash. bash and other shells include facilities for writing programs, called \shell scripts".

Unix 101: Introduction to UNIX (i.e. Unix for Windows Users) Mark Kegel September 7, 2005 1 Introduction to UNIX (i.e. Unix for Windows Users) The cold hard truth · this course is NOT sponsored by the CS dept. · you will not receive any credit at all introduce ourselv

Andreas Wagner, CEO Berlin Office Schiffbauerdamm 19, D-10117 Berlin Phone: 49-30-27595-141 Fax: 49-30-27595142 berlin@offshore-stiftung.de Varel Office Oldenburger Str. 65, D-26316 Varel Phone: 49-4451-9515-161 Fax: 49-4451-9515-249 varel@offshore-stiftung.de www.offshore-stiftung.de More news & information (German/English) 16 Backup Slides German Offshore Windfarms under Construction 2 .