LJ Archive

Listing 5. Shell Example

#Construct SQL, place in $sql
read -d 'EOF' sql<<EOF
select * from person order by lastname
EOF

# Fetch
ret=$(sqlite $db "$sql" 2>&1)
if [ $? = "1" ]
then
    # An error occurred. Print and exit
    printf "Error: $ret.\n"
    printf "SQL: $sql\n"
    exit 1
    else
        # No errors, print the SQL
        for row in $ret
        do
            IFS="|"

            # Set variables from record.
            set $row

            # Assign for convenience.
            firstname=$1
            lastname=$2
            dir=$3
            printf "%-14s %-15s\n" \
                    $firstname $lastname

            unset IFS
        done
fi
LJ Archive