I am taking an INTRODUCTORY Unix/Linux Shell Programming course and need some help with my homework.
Can you please write a test statement using string operators to do the following:
Verify the ordinary file myLog is empty
Verify variable itusIsGood is not null (itusIsGood is the name of the variable)
Verify the value in variable ans is no or the value in variable today is Sunday
Please find the shell programming to check the following validations
Answer1#
test -s myLog && echo "myLog: File is not empty" || echo
"myLog: File is empty"
Answer2#:
test -z $itusIsGood && echo "Variable: itusIsGood is
null" || echo "variable: itusIsGood value is $itusIsGood"
Answer3#:
test "$ans" == "no" -o "$today" == "Sunday" && echo
"either variables have right value" || echo "either variables have
wrong value"
Note: I have added addtional comments and shell script to validate the string operators.
Question# 1: We can "-s" operator to determine whether the string is empty or not.
"-s" usage in Shell Script:
#!/bin/bash
#-s operatir determines whether the file is empty or not
if [ -s myLog ]
then
echo "File is not empty"
else
echo "File is empty"
fi
Screen Shot:

Output:
USER>sh stringOperators.sh
File not empty
USER>
USER>cat myLog
data
USER>>myLog
USER>sh stringOperators.sh
File empty
USER>
USER>
Question# 2: "-z" operator is used to determine whether the variable is null or not. I have given the sample shell script to verify the same.
Shell Program:
itusIsGood=
if [ -z $itusIsGood ]
then
echo "Variable: itusIsGood is null"
else
#you can assign some value to the variable and check the
output
echo "variable: itusIsGood value is $itusIsGood"
fi
Output:
Variable: itusIsGood is null
Screen Shot:

Question# 3: we can user "==" operator to verify the value present in variable to same as given value. Following shell program explains it.
Shell Program:
ans="no"
today="Sunday"
if [ "$ans" == "no" ]
then
echo "variable: ans has $ans"
fi
if [ "$today" == "Sunday" ]
then
echo "variable: today has $today"
fi
Output:
USER>sh stringOperators.sh
variable: ans has no
variable: today has Sunday
USER>
Screen Shot for all the 3 questions:

I am taking an INTRODUCTORY Unix/Linux Shell Programming course and need some help with my homework....