Section 1: getting started with Shell Basics

Keywords: Programming less shell calculator

1, format

  • Opening format: ×! / bin/bash
  • Ending format: × $?

2, parameters

  • $ා is the number of parameters passed to the script
  • $0 is the name of the script itself
  • $1 is the first parameter passed to the shell script
  • $2 is the second parameter passed to the shell script
  • $@ is a list of all parameters passed to the script
  • $* is a single string to display all the parameters passed to the script. Unlike location variables, there can be more than 9 parameters
  • $$is the current process ID number for the script to run
  • $? Is to display the exit status of the last command. 0 means there is no error, and other means there is error

3. echo command

  • echo: line break at the end of output string;
  • echo -n "": output string without line break at the end
  • echo -e "": the following characters appear for interpretation
    • "\ a": warning character;
    • "\ b": backspace;
    • "\ c": ignore the last line break;
    • "\ f": clear screen;
    • "\ n": line feed;
    • "\ r": enter;
    • "\ 0ddd": represents a character as an octal value of 1 to 3 digits

4. Instance code

#!/bin/bash

# 1
#Single quotation mark string
echo 'The time and date are :'
#Use command
date
#Double quotation mark string
echo "Let's see who's logged into the system:"
#Use command
who
#Remove end newline - n
echo -n "Time are:"
date


#2
#Using environment variables
echo "User info for userid: $USER"
echo "UID: $UID"
echo "HOME: $HOME"
#Use escape characters
echo "The cost of the item is \$15"
#User defined variable
days=10
guest="Katie"
echo "$guest checked in $days days ago"


#3
#Backquotes allow commands to be output to variables
testing='date'
echo "The date and time are:"$testing
#Use backquotes to capture the current date and use it to create a unique filename in the script, which is an extract date for the log filename
#Common technology of period information; +% y%m%d format indicates that date command uses two digits to display year, month and day
today='date +%y%m%d'
ls /usr/bin -al > log.$today


#4
#Output redirection command > output file
#Create file, overwrite file content
data >  log.txt
#Attach data to an existing file
data >> log.txt
#Input redirection command < inputfile
#wc command counts the number of lines, words, and bytes of data provided by the built-in input redirection
wc   <  log.txt



#5
#When specifying a number for a variable, use the dollar sign and square brackets ($[operation])
var1 = $[1+5]
echo $var1
var2 = $[$var1 * 2]
echo $var2


#6
#Floating point solution with built-in bash calculator (called bc)
#The bash calculator is actually a programming language that allows floating-point expressions to be entered on the command line and then evaluated
#Use bc: variable='echo "options; expression" | bc 'in your script
#scale: floating point number of output digits
var3 = 'echo "scale=4; 3.44/5" | bc'
echo The answer is $var3

#7
#Use the built-in input redirection method instead of file redirection; built in input redirection allows data to be directed directly from the command line
#The EOF text string indicates the beginning and end of the built-in redirected data
#variable='bc << EOF
#options
#statements
#expressions
#EOF
#'
var4=10.46
var5=43.67
var6=33.2
var7=71
var8='bc << EOF
a1 = ($var4 * $var5)
b1 = ($var6 * $var7)
a1+b1
EOF
'



#8
#The script uses the date command of the if line. If the command is executed successfully, the echo statement will display a text string
if date
then
  echo "it worked"
fi

if hello
then 
  echo "it worked"
else
  echo "it not worked"
fi



#9
#Digital comparison 
#-eq: equal or not; - ge: greater than or equal to; - gt: greater than; - le: less than or equal to; - lt: less than; - ne: unequal
data1 = 10
data2 = 11
if [$data1 -eq $data2]
then 
  echo "The value are equal"
else
  echo "The value are different"
fi
#string comparison
#=: same or not;! =: whether it is different; < whether it is less than; > whether it is greater than;
#-n str: greater than; - z str: 0
testuser=rich
if [@USER != $testuser]
then
  echo "This isn't $testuser"
else
  echo "Welcome $testuser"
fi
#String size comparison requires escape characters or will be considered: redirection characters
str1=baseball
str2=hockey
if [$str1 \> $str2]
then 
  echo "$str1 is greater than $str2"
else
  echo "$str1 is less than $str2"
fi
#File comparison
#-d file: exists and is a directory; - e file: exists; - f file: exists and is a file
#-r: whether it exists and is readable; - s: whether it exists is not empty; - w: whether it exists and is writable; - x: whether it exists and is executable
#file1 -nt file2: is file1 newer than file2? file1 -ot file2: is file1 older than file2;
if [-d $HOME]
then
  echo "Your HOME directory exists"
  cd $HOME
  ls -a
else
  echo "There's a problem with your HOME directory"
fi


#10
#Composite condition check
if [-d $HOME] && [-w $/HOME/testing]
then
  echo "The file exists and you can write to it"
else
  echo "I can't write to the file"
fi


#11
#Double parentheses: allow high-level mathematical formulas ((expression)) in comparison
#val + +: increment after; val --: decrement after; + + val: increment before; -- val: decrement before;! : logical negation
#~: negate; * *: exponentiation; < shift left; > > shift right; &: Boolean logical and; |: Boolean logical or;
#&&: logical and; | |: logical or
data3=10
if (($data3**2 > 90))
then
  ((data4=$data3**2))
   echo "The square of $data3 is $data4"
fi

#12
#Brackets: [[expression]] pattern matching, you can define a regular expression that matches the string value
if [[$USER == r*]]
then
  echo "Hello $USER"
else
  echo "Sorry, I don't know you"
fi

#13
#case statement
case $USER in
rich | barbara)
  echo "welcom, $USER";;
testing)
  echo "please enjoy your visit";;
jessica)
  echo "Don't forget to log off";;
*)
  echo "Sorry";;
esac

#$?

Posted by mapunk on Fri, 08 Nov 2019 08:58:44 -0800