dimanche 8 octobre 2017

IF command executes with FALSE boolean

Say i have

1129|Lepland|Carmen|female|1984-02-18|228T04:39:58.781+0000|81.25.252.111|Internet Explorer

FileInput=false #File Entry(True/False)
FILENAME= #Name of File
IDRequest=false #Request of ID
IDNumber=""
FNRequest=false #Request of Firstnames
LNRequest=false #Request of Lastnames
BSDate=false #Request  of Born Since
BUDate=false #Request of Born Until
BrowserRequest=false #Request of Browser
EditRequest=false #Request for editing

for arg in $@;do
if [ -f  $arg ];then # Check if file argument is given
    FileInput=true
    FILENAME=$arg
fi      
if [ $arg = "-id" ];then #Request for ID
    IDRequest=true
fi
if [ $arg = "--firstnames" ];then #Request for FirstNames
    FNRequest=true
fi
if [ $arg = "--lastnames" ];then #Request for LastNames
    LNRequest=true
fi
if [ $arg = "--born-since" ];then #Request for Born Since
    BSDate=true
fi
if [ $arg = "--born-until" ];then #Request for Born Until
    BUDate=true
fi
if [ $arg = "--browsers" ];then #Request for Browsers
    BrowserRequest=true
fi
if [ $arg = "--edit" ];then #Request for Editing
    EditRequest=true
fi
done

#Check Control Signals
#echo $IDRequest
#echo $FNRequest
#echo $LNRequest
#echo $BSDate
#echo $BUDate   
#echo $BrowserRequest   
#echo $EditRequest



#Main 

if [ FileInput ] && [ $# -eq 2 ];then #./tool.sh -f <file>
cat -n $FILENAME 
elif [ FileInput ] && [ $# -eq 4 ] && [ IDRequest ] ;then # ./tool.sh -f 
<file> -id <id>

while [ $# -gt 0 ]; do #Get and store ID Number
if [ "$1" = -id ]; then
    IDNumber=$2
fi
shift
done
awk -F'|' -v id="$IDNumber" '$1 == id {print $3,$2,$5}' $FILENAME # Print 
FirstName,LastName,BirthDate via ID

elif [ FileInput ] && [ FNRequest ] && [ $# -eq 3 ];then
awk -F'|' '{print $3}'  $FILENAME

elif [ FileInput ] && [ LNRequest ] && [ $# -eq 3 ];then
awk -F'|' '{print $2}'  $FILENAME 

fi

I am asked to print the last names and first names SORTED alphabetically.I have two questions. How do i sort them ? And also whenever i run the program even if i request the lastname it runs this if and prints the firstNames:

elif [ FileInput ] && [ FNRequest ] && [ $# -eq 3 ];then
awk -F'|' '{print $3}'  $FILENAME

even tho FNRequest is FALSE. Why is that ?

Aucun commentaire:

Enregistrer un commentaire