1 / 37

Introduction to Linux and Shell Scripting

Introduction to Linux and Shell Scripting. Jacob Chan. Review on Linux Commands. Windows vs Linux Stability Windows often crashes; Linux rarely does Bug potential Cost Learnability Interface Market prowess Windows for games, Linux for industry Windows’ customer service features

amadis
Download Presentation

Introduction to Linux and Shell Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Linux and Shell Scripting Jacob Chan

  2. Review on Linux Commands • Windows vs Linux • Stability • Windows often crashes; Linux rarely does • Bug potential • Cost • Learnability • Interface • Market prowess • Windows for games, Linux for industry • Windows’ customer service features • TIP: DON’T ALWAYS RELY ON THIS

  3. Review on Linux Commands • Some important commands in Linux • File commands file <filename> (print file type) stat <filename> (print more detailed file information) cat <filename> (view file contents) touch <filename> (create new blank file) gedit <filename> (create new blank file, but it is not yet saved • Directory commands mkdir cd <directory> ls <argument> <directory> (argument can be –l, -I, -a)

  4. Review on Linux Commands • Some important commands in Linux • System commands exit pwd (print working directory) who (displays currently logged in users) whoami (current user displayed) • Manual pages man <command> (opens manual page of command) info <command> (works like man but has link to other pages)

  5. Review on Linux Commands • Some important commands in Linux • File data manipulation wc grep sort head cut • File access chmod <permission> <file> umask <permission> ln –s <file> <link>

  6. Review on Linux Commands • Other commands • Regular Expressions (*, [0-9], ?) • I/O Redirection <command> <file> > <newFile> <command> < <file>

  7. Review on Linux Commands • Other commands • Pipes | are used for combining different commands at once • Example: Create a file containing the top 10 foods you like • Then, type this command: cat food.txt | grep “b” | sort What happens?

  8. Review on Linux Commands • Running processes in the background • Sometimes, when we run gedit, we can’t type on the command line • Solution: put & • This way, you can work on your text file AND type at the command line at the same time 

  9. Review on Linux Commands • Processes • There is an ID for every process running • Viewing PID’s ps -ax • Killing processes (terminates the process no matter what) kill -9 <pid>

  10. Now What? • Typing all these commands is a hassle • Piping is a solution, but you don’t want to pipe a lot of commands together! • This is especially true in handling multiple files at the same time • Database management • Running executables • Backups

  11. A more efficient way: SHELL • /bin/bash or /bin/sh (Bourne again shell) • Automates processes that are typed over and over • Works like .bat files (for Windows users) • But Windows has a tendency to make “weird” changes from those .bat files • In Linux/Unix, you won’t get that • Shell files are special text files • Usually ends in .sh • First line: #!/bin/bash • Helps in identifying that the file is shell script (comment)

  12. Shell Script • Typical content of shell • Text • Write first shell script #!/bin/bash pwd ls –l echo “hello world” touch pogi.txt

  13. Shell Script • Running shell • Try typing the shell file first. • Then try typing ./before writing the shell file • Reason why latter worked: system issues • The shell script is not included in the path variable, so it won’t work by default • Same goes with executables Good job on your first shell script! 

  14. Shell Programming • Variables • Some variables are pre-defined by the system (RESERVED) • PATH (list of directories searched when system looks for command) • TERM (name of current terminal) • HOME (user’s home directory) • LOGNAME (user’s login name) • SHELL (name of shell program) • How to use variables a = <something>

  15. Shell Programming • Variables • Access variable value • Use $ or ${} • Access program output • Use $(programName) or “ ‘programName’ “ • Displaying value of variables: echo $PATH echo ${PATH} • Variables can either be used as command name or argument cp $file ${file}02$CC myprog.c -o myprog

  16. Shell Programming • Accessing program output example login=$(cat /etc/passwd | grepuser| cut -d: -f1)echo ${login}today=`date | cut -d\ -f1-2`echo $todayecho ${today} > /tmp/today.txt

  17. Shell Programming • Quoting • These characters have special meaning for shell (space, &, < > $ * “ ‘) • How to go around them: \ cat Your\ Mom Mkdir food\&bar • Single quotes suppresses special meanings Example: help=‘Donuts $35.00 & Brownies $20.00’ • BUT double quotes won’t suppress meaning of $ Example: PATH = “$Path:/home”

  18. Shell Programming • Quoting Test • Given that foo = “bar”, what is the result of the following: echo “$foo” echo \$foo echo $’foo’ echo $foo

  19. Shell Programming • Conditionals • If Statements if command1 then command2 fi if command1; then command2; fi if command1; then command2 else command3 fi

  20. Shell Programming • Conditionals • example of if statement read ansif [ “$ans” = “y” ]; then echo “Your answer is yes”else echo “Your answer is not yes”fi The [] command is like the test command (like a shortcut for checking booleans)

  21. Shell Programming • Conditionals • The “normal” if (the one we usually know) if command1; then command2 elif command3; then command4 else command5 fi

  22. Shell Programming • Conditionals • The case statement (no switch required) case WORD in (PATTERN) commands ; ; (PATTERN) commands ; ; (PATTERN) commands ; ; esac

  23. Shell Programming • Loops • For loops for <item> in <list>; do commands done #will check for each item in list #useful for processing each file in directory #list is either a command in Linux, a list (or sequence) of numbers or a plain list #curly braces are optional for list (and are used for numbers only)

  24. Shell Programming • Loops • Example of for loop for file in /etc/* do echo “${file}” done

  25. Shell Programming • Loops • While loop while [ condition ] do command1 command2 command3 done

  26. Shell Programming • Loops • Until loop (works like while except that statement will be executed until condition is TRUE (therefore, it will execute when FALSE) until [ condition ] do command1 command2 command3 done

  27. Shell Programming • Reading input • Use either the read command • Example #!/bin/bash echo “what is your name?” read name echo “hello $name”

  28. Shell Programming • Scripts are executable files. So to run them, make the file executable. How? chmod +x <file>

  29. PROGRAMMING EXERCISE • Try creating a program that displays the date today, and checks if the time now is in the morning, afternoon, or evening. (6am to 12pm is morning, 12pm to 6pm is afternoon, 6pm to 6am is night). Print the appropriate greeting (Good morning, good afternoon, good evening) • Clue: find a way on how to print out the date (it was already discussed a while ago)

  30. LAB 1: Printing File Details • Create a lab filedetails.sh that describes a file as detailed as possible. The command will be run as ./filedetails.sh NAME where NAME is the name of the file we want details of. • Wildcards will also be considered, meaning I can type ./filedetails.sh * (which prints out details of every file • Note: you also need to know how to get file types (if it is a device file, regular file, directory, or a symbolic link

  31. LAB 1: Printing File Details • Regular Files(and other file types) File Name: File Type: Owner: Permissions for Owner: Group: Permissions for Group: Permissions for Others: Actual size: Size on Disk: Inode No.: N of Links: Access Date/Time: Modify Date/Time: Change Date/Time:

  32. LAB 1: Printing File Details • Device Files File Name: File Type: Owner: Permissions for Owner: Group: Permissions for Group: Permissions for Others: Block or Char? Device Major: Device Minor: Inode No.: N of Links: Access Date/Time: Modify Date/Time: Change Date/Time:

  33. LAB 1: Printing File Details • Symbolic Link File Name: File Type: Link to Pathname?

  34. LAB 1: Printing File Details • Directories File Name: File Type: Owner: Permissions for Owner: Group: Permissions for Group: Permissions for Others: Inode No.: N of Links: Access Date/Time: Modify Date/Time: Change Date/Time:

  35. LAB 1: Printing File Details • Deadline of submission: NEXT WEEK • Links are provided for Sections A and B. • Put your filedetails.sh in a folder, along with a CERTIFICATE OF AUTHORSHIP (DO NOT FORGET THIS) • Filename: CS162B_<Section>_Lab1_<Surname>_<IDNumber>.tar • Submit on the link provided for each class. Each link I will post a password 

  36. NEXT MEETING • C Basics • More programming and LAB

  37. THE END No dream is too big and no dreamer is too small

More Related