Skip to content

8. Command Shell

Introduction to Mac Shell Scripts 1

  • The Default shell in Mac is zsh (Z shell); but historically, it was bash (Bourne Again Shell).
  • sudo is short of superuser do.
  • Components of a bash script:
    • Options: options can be passed to the script when running it. eg: ./script.sh -a -b -c.
    • Variables. variable names are case sensitive. as var="hello" and VAR="hello". no space is allowed around =. eg: var="hello".
    • Conditionals. eg: if [ $var = "hello" ]; then echo "hello"; fi.

Script Examples

  • Remove user from admins group
#!/bin/bash

# Get the currently logged in username.
loggedInUser=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')


# Use the dseditgroup command to remove the user from the admin group.
/usr/sbin/dseditgroup -o edit -d "$loggedInUser" -t user admin

PowerShell 2

  • PowerShell is a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS.
  • PowerShell Command-line Shell:
    • accepts texts and returns texts or objects.
    • features: history, tab completion, command-line editing, and command aliasing.
  • PowerShell Scripting Language:
    • Used in automation, CI/CD, and DevOps.
    • Features: OOP, formatted output, dynamic type system, and built-in support for files like XML, JSON, and YAML.
  • PowerShell Configuration Management Framework:
    • Used to manage and configure systems.
    • Features: Desired State Configuration (DSC), and Package Management.

Bash 3

  • Advantages of Bash:
    • Access OS more efficiently.
    • Less resource-intensive.
    • Powerful in dealing with text files.
    • Automate tasks.
    • Run any programming language, by executing node x.js and then python y.py in the same script.

Bash Scripting Tutorial 4

  • Local vs Global Variables:
    • Local variables are only available within the function, defined using the local keyword.
    • Global variables are available everywhere.
  • Command substitution: $(command):
    • also called, sub-shelling.
    • The command is executed in a sub-shell (another process), and the output is returned to the parent shell.
  • Reading user input:
    • read -p "Enter your name: " name
    • read -sp "Enter your password: " password
  • Trap commands:
    • trap command allows you to define a handler for a signal.
    • The defined handler will be executed when the signal is received, instead of the default action.

arrays

#!/bin/bash

#Declare array with 4 elements
ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux )

# get number of elements in the array
ELEMENTS=${#ARRAY[@]}

# echo each element in array
# for loop
for (( i=0;i<$ELEMENTS;i++)); do
    echo ${ARRAY[${i}]}
done

read files into a bash array

#!/bin/bash

# Declare array
declare -a ARRAY

# Link filedescriptor 10 with stdin
exec 10<&0

# stdin replaced with a file supplied as a first argument
exec < $1
let count=0

while read LINE; do
    ARRAY[$count]=$LINE
    ((count++))
done

echo Number of elements: ${#ARRAY[@]}

# echo array's content
echo ${ARRAY[@]}

# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-

Bash file test operators

  • In Bash, we can test to see the different characteristics of a file or directory. See the table below for a full list.
Operator Description
-b file True if file exists and is a block special file.
-c file True if file exists and is a character special file.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
  • The Complete list is here.

The Select command

  • The select command is used to create a menu system. It is similar to the case statement, but it is more user-friendly.
  • example: select word in "linux" "bash" "scripting" "tutorial"
#!/bin/bash

PS3='Choose one word: '

# bash select
select word in "linux" "bash" "scripting" "tutorial"
do
  echo "The word you have selected is: $word"
# Break, otherwise endless loop
  break
done

exit 0

Introduction to the Linux Shell and Command Line 5

  • Command structure: command [options] [arguments] eg: ls -l /home.
  • To get help on a command, use man command. eg: man ls or command --help

Reference


  1. Guide for Apple IT: Introduction to Mac scripting. (2020, January 14). Kandji. https://blog.kandji.io/guide-for-apple-it-introduction-to-mac-scripting#:~:text=Shells%20are%20command%20line%20interpreters,shell%20or%20command%2Dline%20interpreter 

  2. hananyajacobson, sdwheeler, & shawnkoon. (2022, May 18). What is PowerShell? Microsoft Docs. https://docs.microsoft.com/en-us/powershell/scripting/overview?view=powershell-7.2 

  3. Klein, M. (2022, March 1). What is bash used for? Codecademy News. https://www.codecademy.com/resources/blog/what-is-bash-used-for/ 

  4. Reynolds, L. (2022, February 12). Bash Scripting Tutorial. LinuxConfig. https://linuxconfig.org/bash-scripting-tutorial 

  5. Oracle Learning. (2021, May 24). Introduction to the shell and command line [Video]. YouTube. https://www.youtube.com/watch?v=QQSI_901fUU