OneCompiler

Shell Scripting

1662

US1

echo "Enter First Name:"
read first_name
echo "Enter Last Name:"
read last_name
echo "Enter Date of Birth (YYYY-MM-DD):"
read dob
echo "Enter Email ID:"
read email_id
echo "Enter Contact No:"
read contact_no

awk -v f="$first_name" -v l="$last_name" -v d="$dob" -v e="$email_id" -v c="$contact_no" \
'BEGIN { print f "|" l "|" d "|" e "|" c >> "passenger_register.txt" }'

<------------------------------------------------------------>
US2
#!/bin/bash

awk 'BEGIN {
    print "FlightName|Origin|Destination|TravelDate|Price" >> "flight_details.txt";
    print "SpiceJet757|CHN|HYD|3Jan|10000" >> "flight_details.txt";
    print "AkasaAir123|DEL|BGL|3Jan|9000" >> "flight_details.txt";
    print "Indigo667|DEL|HYD|3Jan|13000" >> "flight_details.txt";
    print "AIRIndia667|DEL|HYD|3Jan|16000" >> "flight_details.txt";
}'


cat flight_details.txt

#!/bin/bash

echo "Enter Origin (From):"
read origin
echo "Enter Destination (To):"
read destination
echo "Enter Travel Date (e.g., 3Jan):"
read travel_date

echo "Matching Flights:"
awk -F"|" -v orig="$origin" -v dest="$destination" -v date="$travel_date" '
    $2 == orig && $3 == dest && $4 == date {
        printf "Flight: %s, Origin: %s, Destination: %s, Date: %s, Price: %s\n", $1, $2, $3, $4, $5
    }
' flight_details.txt

<------------------------------------------------------------>
US3

echo "Available Flights (Sorted by Price in Descending Order):"
awk -F"|" 'NR > 1 {print $0}' flight_details.txt | sort -t "|" -k5,5nr | while IFS="|" read -r flight_name origin dest date price
do
    echo "Flight: $flight_name, Origin: $origin, Destination: $dest, Date: $date, Price: $price"
done

<------------------------------------------------------------>
US4 
#!/bin/bash

#!/bin/bash

file="flight_details.txt"

echo "Enter Flight Name:"
read flight_name
echo "Enter Origin (From):"
read origin
echo "Enter Destination (To):"
read destination
echo "Enter Travel Date (e.g., 3Jan):"
read travel_date
echo "Enter Price:"
read price

# Use awk to append the data to the file
awk -v flight="$flight_name" -v orig="$origin" -v dest="$destination" -v date="$travel_date" -v price="$price" \
'BEGIN {
    print flight "|" orig "|" dest "|" date "|" price >> "'"$file"'"
}' 

echo "Flight details saved successfully!"


<------------------------------------------------------------>
US5
#!/bin/bash

#!/bin/bash

file="flight_details.txt"

echo "Enter Destination (To):"
read destination

echo "Flights to $destination:"

awk -F"|" -v dest="$destination" '$3 == dest { print }' $file