Bash, In A NutShell RUSTY MYERS - SYSTEMS ADMINISTRATOR, PENN STATE CHRIS LAWSON - IT CONSULTANT/SUPPORT SPECIALIST, PENN STATE ALTOONA

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

Linux Command Line Primer. By: Scott Marshall

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

Introduction: What is Unix?

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CS CS Tutorial 2 2 Winter 2018

Introduction to the UNIX command line

Introduction to Linux for BlueBEAR. January

Basic Brilliant Scripting for Beginners. Bryce Carlson

Linux Systems Administration Getting Started with Linux

Introduction to Linux

Linux Shell Script. J. K. Mandal

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

Introduction of Linux

Open up a terminal, make sure you are in your home directory, and run the command.

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

CISC 220 fall 2011, set 1: Linux basics

The Linux Command Line & Shell Scripting

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

Introduction to Linux

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

Introduction To. Barry Grant

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Useful Unix Commands Cheat Sheet

Lab Working with Linux Command Line

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

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Unix Introduction to UNIX

Introduction to UNIX Command Line

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

Perl and R Scripting for Biologists

Exploring UNIX: Session 3

Operating Systems. Copyleft 2005, Binnur Kurt

Introduction to Linux Workshop 1

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Week 2 Lecture 3. Unix

Introduction to Linux Part 1. Anita Orendt and Wim Cardoen Center for High Performance Computing 24 May 2017

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

Scripting OS X. Armin Briegel. Mac Admin, Consultant and Author

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Linux & Shell Programming 2014

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

Chapter 4. Unix Tutorial. Unix Shell

Recap From Last Time: Setup Checklist BGGN 213. Todays Menu. Introduction to UNIX.

COMS 6100 Class Notes 3

Introduction to the shell Part II

Introduction to the Linux Command Line

Introduction to Linux. Fundamentals of Computer Science

Chap2: Operating-System Structures

Overview LEARN. History of Linux Linux Architecture Linux File System Linux Access Linux Commands File Permission Editors Conclusion and Questions

CS Unix Tools & Scripting

Basic Linux Command Line Interface Guide

EE516: Embedded Software Project 1. Setting Up Environment for Projects

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Basic Linux (Bash) Commands

Basic Linux Command Line Interface Guide

Linux File System and Basic Commands

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

User Guide Version 2.0

Introduction to Unix and Linux. Workshop 1: Directories and Files

Linux at the Command Line Don Johnson of BU IS&T

Computer Systems and Architecture

Short Read Sequencing Analysis Workshop

Introduction to remote command line Linux. Research Computing Team University of Birmingham

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

unix intro Documentation

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

CHE3935. Lecture 1. Introduction to Linux

Getting Started With Cpsc (Advanced) Hosted by Jarrett Spiker

Introduction to Linux

Advanced Linux Commands & Shell Scripting

Working with Basic Linux. Daniel Balagué

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

Introduction to Linux

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Recap From Last Time:

The Unix Shell & Shell Scripts

BGGN 213 Working with UNIX Barry Grant

CS Fundamentals of Programming II Fall Very Basic UNIX

Linux Essentials Objectives Topics:

Computer Systems and Architecture

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

Linux Kung Fu. Ross Ventresca UBNetDef, Fall 2017

Introduction to Supercomputing

Linux Bootcamp Fall 2015

Linux for Beginners. Windows users should download putty or bitvise:

Essential Linux Shell Commands

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

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

Chapter-3. Introduction to Unix: Fundamental Commands

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

A Brief Introduction to the Linux Shell for Data Science

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

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

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

A Brief Introduction to Unix

Unix basics exercise MBV-INFX410

Unix Handouts. Shantanu N Kulkarni

Transcription:

Bash, In A NutShell RUSTY MYERS - SYSTEMS ADMINISTRATOR, PENN STATE CHRIS LAWSON - IT CONSULTANT/SUPPORT SPECIALIST, PENN STATE ALTOONA

BASH, IN A NUTSHELL What is bash? Brian Fox 1989 Bourne Again Shell (bash) Command Interpreter A shell program that interprets commands and executes them. Kernel: Core of the OS (Mac OS X Mach kernel) Shell: Outer layer of OS that interacts with the user, sending requests to the kernel for execution. Binary at /bin/bash Responsible for spawning subshells

Follow Along! Open Terminal.app & Play Around.

BASH, IN A NUTSHELL Terminal.app /Applications/Utilities/ Lots of various commands available. Examples man Help manual. cp Copy one or more files to another location. mkdir Create a new directory/folder mv Move or rename files or directories chmod Change access permissions install Copy files and set attributes shutdown Shutdown or restart OS

TAKING GUI PROCESSES TO TERMINAL

SYNTAX OF A COMMAND ls -la ~/Desktop/ ls = Command or Utility (List files/directories) -la = Options or Flags (-l = List in long format / -a = Include directory/hidden entries) ~/Desktop = Argument (Which directory to pull the information from) * ~ = Home directory * Options or flags can also have arguments depending on the command.

USEFUL TIPS/SHORTCUTS ~ = Shortcut for Home directory tab complete = tap tab to complete text. Type ~/Des and hit tab to complete ~/Desktop/ = Toggle through previous commands in that session.!(command) = Will read your history and run the most recent run of the matched command.!! = Runs the last command Control + L (or Command+K) = Clear Terminal window. Control + C = Stop the current process.

USEFUL COMMANDS # Bring up manual for ping command. $ man ping # Send packets over the network to test connectivity. $ ping -c 5 www.apple.com # Watch for packets transmitted and received and packet loss. # Check network status. $ ifconfig

DIRECTORIES # Print working directory. $ pwd # Change to another. $ cd /Users/$USER/Desktop/ # Print working directory. $ pwd # Should be /Users/$USER/Desktop/

CREATING DIRECTORIES # Create a single directory on the desktop. $ mkdir ~/Desktop/Test1/ # Create a directory structure including all parent directories. $ mkdir -p ~/Desktop/Test1/Test2/Test3/ # Create a directory structure including all parent directories setting permissions to read, write, and execute for the owner, the group and everyone else for the Test3 directory. $ mkdir -pm 777 ~/Desktop/Test1/Test2/Test3/

MAKING/EDITING A FILE # Create the test.txt file in the Test1 directory. $ touch ~/Desktop/Test1/test.txt # Read the file. $ cat ~/Desktop/Test1/test.txt #Append Hello World in test.txt $ echo Hello World >> ~/Desktop/Test1/test.txt # Display file. Hit q to quit. $ less ~/Desktop/Test1/test.txt

MOVING FILES/DIRECTORIES # Moving a single file to another directory $ mv ~/Desktop/Test1/test.txt ~/Desktop/Test1/Test2/Test3/ # Moving a single Directory. $ mv ~/Desktop/Test1/Test2/Test3/ ~/Desktop # Rename a directory (or file) using the mv command. $ mv ~/Desktop/Test1/Test2/ ~/Desktop/Test1/Test4/

DELETING OLD FILES # Delete One File $ rm /Users/$USER/Library/Preferences/com.apple.Safari.plist # Delete One File From All Homes $ sudo rm /Users/*/Library/Preferences/com.apple.Safari.plist # Delete All Files in Caches $ rm -R /Users/$USER/Library/Caches/

USING GOOGLE TO FIND THE WAY Search Terms +terminal +osx +mac Additional Resources http://www.defaults-write.com http://apple.stackexchange.com http://hints.macworld.com

MORE GUI TO TERMINAL

RUN SOFTWARE UPDATES # List all available updates. $ softwareupdate -l # Each update specified by args is downloaded but not installed. $ softwareupdate -d # Each update specified by args is downloaded and installed. $ softwareupdate -i # Enable or Disable Scheduled Check of Software Updates $ softwareupdate --schedule [on off]

INSTALLING SOFTWARE # Install Pkg On Desktop $ installer -pkg ~/Desktop/VLC-2.2.4.pkg -target / # Print the last 100 lines of the install log to confirm success $ tail -100 /var/log/install.log # Waiting and monitoring the install process. $ tail -f /var/log/install.log

GETTING SYSTEM INFORMATION # List profile information $ system_profiler # List all data types available. $ system_profiler -listdatatypes # Get computer serial number $ system_profiler SPHardwareDataType awk -F ': ' /Serial Number/ {print $2}' C04N51BFG9QK

FINDING FILES # Find files matching case insensitive term $ find ~/ -iname "*.sh" # Find Files Modified > 3 Days Ago (DISCLAIMER: DO NOT RUN THIS COMMAND. It will find and erase all files modified or created in the last 3 days.) $ find ~/ -mtime +3d -print0 xargs -0 rm # Use metadata search # List kmditemcontenttype $ mdls mcxtoprofile.py $ mdfind -onlyin ~/ -name.sh kmditemcontenttype = "public.python-script" # Find PDFs in my Home $ mdfind -onlyin ~/ "kmditemcontenttype == 'com.adobe.pdf'"

PREFERENCES # Watch the file system for changes. sudo fs_usage -w grep -i TextWrangler grep -i plist 18:50:39.330969 WrData[AT2] D=0x04d77428 B=0x2000 /dev/disk1 /Users/rzm102/Library/Preferences/com.barebones.textwrangler.plist.TDKJ8ly # File System Writing Data: "WrData[AT2]" # To a Plist: "/Users/rzm102/Library/Preferences/ com.barebones.textwrangler.plist.tdkj8ly" # Domain: com.barebones.textwrangler $ defaults read com.barebones.textwrangler UserName Rusty Myers $ defaults write com.barebones.textwrangler UserName "TheSpider" # Create a profile! https://github.com/timsutton/mcxtoprofile

COMBINING CODE INTO A SCRIPT

EDITING A SCRIPT Script Editors Nano Vim (Vi) Atom TextWrangler

CREATING A SCRIPT #!/bin/bash # Basic script echo "Hello World"

RUNNING A SCRIPT $ chmod u+x script.sh $ chmod 755 script.sh $ /bin/bash script.sh Hello World $./script.sh Hello World $ /Users/rzm102/script.sh Hello World

AUTOMATING THROUGH BASH SCRIPTING

DATE & TIME #!/bin/bash # Time Zone Setup systemsetup -settimezone America/New_York # Set to use Network Time Server clock.psu.edu systemsetup -setusingnetworktime on systemsetup -setnetworktimeserver clock.psu.edu echo "Network Time Configured"

INSTALL SOFTWARE #!/bin/bash curl -o /tmp/office2016.company.pkg http://company.com/pkgs/office2016.pkg installer -pkg /tmp/office2016.company.pkg -target / curl -o /tmp/perfectsoftware.company.pkg http://company.com/pkgs/perfectsoftware.company.pkg curl -o /tmp/coupon.company.pkg http://company.com/pkgs/coupon.company.pkg for mypkg in /tmp/*.pkg; do installer -pkg "$mypkg" -target / done

CHANGE PREFERENCES #!/bin/bash # Skip First Run for All Apps $ domains="com.microsoft.outlook com.microsoft.powerpoint com.microsoft.excel com.microsoft.word com.microsoft.onenote.mac" $ for i in $domains ; do defaults write $i ksubuiappcompletedfirstrunsetup1507 -bool true done https://macops.ca/disabling-first-run-dialogs-in-office-2016-for-mac

VARIABLES

QUOTES Double Quotes " " = Escape all except: $ ` \ \ = Escapes next character Single Quotes ' ' = Escapes all Internal file separator is whitespace

QUOTES $ echo $PWD /Users/student $ echo "$PWD" /Users/student $ echo '$PWD' $PWD $ echo \$PWD $PWD

VARIABLES # Create new variable using alphanumeric $ message="hello" $ ending="world" $ echo "$message" "$ending" Hello World # List all built in variables $ env # List the user name and home folder. $ echo "$USER - $HOME"

GOING FROM OUTPUT TO VARIABLES # Create new variable using $( command ) $ date Wed Jun 22 21:28:02 EDT 2016 $ today=$(date) $ echo $today Wed Jun 22 21:28:52 EDT 2016

GOING VARIABLES TO DECISIONS # Decide what to do based on model type macmodel=$(system_profiler SPHardwareDataType awk '/Model Identifier/ { print $3 }') $ echo $macmodel MacBookPro11,3 $ if [[ "$macmodel" == *Book* ]]; then > echo "This is a portable" > fi This is a portable $

CATCHING ERRORS

EXIT STATUS $ cat script.sh #!/bin/bash echo "ERROR" exit 2 $./script.sh ERROR $ echo $? 2

CHECK FOR FAILURE OR SUCCESS #!/bin/bash diskutil erasedisk HFS+ Untitled disk1 exitvalue=$(echo $?) echo "Disk Utility Exit Value: $exitvalue" if [ $exitvalue -gt 0 ]; then echo "Disk Utility did not complete successfully" else echo "Disk Utility completed successfully" fi

AND OR # Make a Beep if erasedisk fails $ diskutil erasedisk disk1 osascript -e 'beep' # Copy new profile to MDM server $./mcxtoprofile.py --plist com.barebones.textwrangler.plist --identifier rzm102textwrangler \ && scp rzm102textwrangler rzm102@profileserver.com:/mdm/profiles/

SHELLCHECK.NET

# Questions? # What do you want to script?

MacAdmins2016$ /bin/bash /Users/Rusty&Chris/BashInANutShell.sh Thanks For Coming! MacAdmins2016$ echo $? 0