Coding Practice

Project - Linux Toolbox | Based on Shell Script

Table of Content

Chapter 1 Introduction

  1.1 Introduction

  1.2 Design Goals/Objective

Chapter 2 Design/Development/Implementation of the Project

  2.1 Design

  2.2 Code Implementation

Chapter 3 Performance Evaluation

  3.1 Simulation Environment/ Simulation Procedure

  3.2 Results and Discussions

  3.3 Analysis and Outcome

  3.4 Limitations

Chapter 4 Conclusion

  4.1 Introduction

  4.2 Practical Implications

  4.3 Scope of Future Work

References

Chapter 1

Introduction

1.1 Introduction

Linux Toolbox is a tool for Linux operating systems, which allow to check the different type of system information. For example - date, time, device owner, disk usages, memory usages, CPU usages and etc. Another feature of Linux Toolbox is - it will show the current monthly calendar for any year, it can manage a huge of contact information, and it can create a notebook.

1.2 Design Goals/Objective

The sole intention behind the consideration of this project is to generate and manage to show some system information. This project has been developed considering note and contact information keeping the context of the user in mind. Here, the data has been stored in a few text files in the storage.

Chapter 2

Design/Development/Implementation of the Project

2.1 Design

To develop Linux Toolbox, we use Visual Studio Code IDE for code edit. The Linux Toolbox is a simple GUI-based application that has been developed in Shell Script. It is allowed to show system information, enables a store of notebook and contact information. Those are essentially databases that track all information for a user and a user can avail the benefits of using this application. Here, linux_toolbox.sh is the main file. There is also have a file calendar.sh that can show a month of calendar for the user.

2.2 Code Implementation

In this application, users can sign-up, log-in and see the monthly calendar. Also, users can use notebooks and add favorite contact information. We worked with Bash language and use Graphical User Interface via the YAD library. In the case of Bash/Shell Script, we have used some functions such as calendar, home, notebook, view_all_note, contact, and etc which have been used to reduce the code space. Click below to jump in the source code-

Source Code - Linux Toolbox
#!/bin/bash

FILE=$(pwd)/admin.log
LOGGEDIN=$(pwd)/loggedin.log

# bash functions start
#calendar window
function calendar(){
   yad --center --borders=10 --width=420 --title="Linux Toolbox - Calendar" --calendar --button=Back:0

   if [ ${?} -eq 252 ]; then
       echo "Linux Toolbox closed by user"
       exit
   fi
}

function view_all_note() {
    echo "View all note"

    # Get all note from file
    note_file=$(pwd)/notebook.txt

    if [ -f "$note_file" ]; then
        note_text=$(cat notebook.txt)
        notebook=$(yad --scroll --width=450 --height=500 --title="Linux Toolbox - Notebook" --borders=10 --center --separator="ㅤ" \
        --text-align=center \
        --text="<span><b><big><big>My Notes</big></big></b></span>" \
        --form \
        --field="$note_text":LBL \
        --button="Back:0" \
        )
        btn_click=$?

        if [ $btn_click == 0 ]; then
            echo "Back to Notebook"
        elif [ $btn_click == 252 ]; then
            echo "Linux Toolbox closed by user"
            exit
        fi
    else
        echo "No note found"
        notebook=$(yad --scroll --width=450 --height=500 --title="Linux Toolbox - Notebook" --borders=10 --center --separator="ㅤ" \
        --text-align=center \
        --text="<span><b><big><big>My Notes</big></big></b></span>" \
        --form \
        --field="No note found":LBL \
        --button="Back:0" \
        )
        btn_click=$?

        if [ $btn_click == 0 ]; then
            echo "Back to Notebook"
        elif [ $btn_click == 252 ]; then
            echo "Linux Toolbox closed by user"
            exit
        fi
    fi
}

function notebook(){

    while :
    do
        notebook=$(yad --width=450 --height=500 --title="Linux Toolbox - Notebook" --borders=10 --center --separator="ㅤ" \
        --text-align=center \
        --text="<span><b><big><big>Create Your Note</big></big></b></span>" \
        --form --field="<b>Title</b>": \
        --field="":TXT \
        --button="Add:2" \
        --button="View:3" \
        --button="Back:0" \
        )
        btn_click=$?

        if [ $btn_click == 0 ]; then
            echo "Back to home"
            break
        elif [ $btn_click == 2 ]; then
            title=$(echo $notebook | awk -F "ㅤ" '{print $1}')
            notebook=$(echo $notebook | awk -F "ㅤ" '{print $2}')
            break="\n----------------------------------------------------------------------------------------------------------\n"
            echo "Title: $title\nNote: $notebook $break" >> notebook.txt
            echo "Note added"
        elif [ $btn_click == 3 ]; then
            echo "View Note"
            view_all_note
        fi
    done
}

# contact window start
function add_contact(){

    contact_value=$(yad --form --center --borders=10 --width=380 --height=280 --title="Linux Toolbox" --separator='ㅤ' \
    --text-align=center --text="<span><b><big><big>Add Contact</big></big></b></span>"\
    --field="  <b>Name:</b>":LBL \
    --field="" \
    --field="  <b>Phone:</b>":LBL \
    --field="" \
    --field="  <b>Email:</b>":LBL \
    --field="" \
    --button=Add:0 \
    --button=Back:1 \ ) valid=${?}

    code=${?}

    if [ $code == 1 ]; then
        echo "Back to the contact window"
    elif [ $code == 0 ]; then
        echo "$contact_value"
        echo "Add contact"

        name=$(echo $contact_value | awk -F "ㅤ" '{print $2}')
        phone=$(echo $contact_value | awk -F "ㅤ" '{print $4}')
        email=$(echo $contact_value | awk -F "ㅤ" '{print $6}')
        break="------------------------------------------------"
        echo -e "Name: $name | Phone: $phone | Email: $email\n" >> contact_list.log
        add_contact
    fi
}

function phone_number_not_found(){
    echo "Phone number not found"
    $(yad --center --borders=10 --width=350 --height=300 --title="Linux Toolbox" --text="<span><b><big><big>Phone number not found</big></big></b></span>" \
    --button="Back":1 \ )
    code=${?}

    if [ $code == 1 ]; then
        echo "Back to home window"
    elif [ $code == 252 ]; then
        echo "Linux Toolbox closed by user"
        exit
    fi
}

function update_contact(){

    echo "Update contact"

    update_string=$(yad --form --center --borders=10 --width=350 --height=300 --title="Linux Toolbox" --separator='ㅤ' --text-align=center --text="<span><b><big><big>Update Contact</big></big></b></span>" \
    --field="<b>Old Phone:</b>":LBL \
    --field="" \
    --field="<b>New Name:</b>":LBL \
    --field="" \
    --field="<b>New Phone:</b>":LBL \
    --field="" \
    --field="<b>New Email:</b>":LBL \
    --field="" \
    --button="Update":0 \
    --button="Back":1 \ )

    code=${?}

    if [ $code == 1 ]; then
        echo "Back to home window"
    elif [ $code == 0 ]; then
        echo "Update string: $update_string"
        
        old_phone=""
        

        entry_phone=$(echo $update_string | awk -F "ㅤ" '{print $2}')
        full_string=$(grep -i "$entry_phone" contact_list.log)
    
        old_name=$(echo $full_string | awk '{print $2}')
        old_phone=$(echo $full_string | awk '{print $5}')
        old_email=$(echo $full_string | awk '{print $8}')

        echo "Full string: $full_string"
        echo "Old phone: $old_phone"
        echo "Entry phone: $entry_phone"

        if [ ! -z "$old_phone" ]; then
            if [ $entry_phone == $old_phone ]; then
                echo "Phone number found"

                new_name=$(echo $update_string | awk -F "ㅤ" '{print $4}')
                new_phone=$(echo $update_string | awk -F "ㅤ" '{print $6}')
                new_email=$(echo $update_string | awk -F "ㅤ" '{print $8}')

                if [ ! -z "$new_name" ]; then
                    sed -i "s/$old_name/$new_name/g" contact_list.log
                fi
                if [ ! -z "$new_phone" ]; then
                    sed -i "s/$old_phone/$new_phone/g" contact_list.log
                fi
                if [ ! -z "$new_email" ]; then
                    sed -i "s/$old_email/$new_email/g" contact_list.log
                fi
                echo "Contact Updated"
            else
                phone_number_not_found
            fi
        else
            phone_number_not_found
        fi
    elif [ $code == 252 ]; then
        echo "Linux Toolbox closed by user"
        exit
    fi
}

function view_contact(){
    
    echo "View contact"
    v_contact=$(nl -s '. ' -w 1 contact_list.log)

    $(yad --form --center --borders=10 --width=350 --height=300 --title="Linux Toolbox" --separator=' ' --text-align=center --scroll --text="<span><b><big><big>Contact List</big></big></b></span>"\
    --field="":LBL \
    --field="$v_contact":LBL \
    --button="Add":1 \
    --button="Update":2 \
    --button="Search":3 \
    --button="Delete":4 \
    --button="Back":0 \ )

    code=${?}
    
    if [ $code == 0 ]; then
        echo "Back to manage contact window"
    elif [ $code == 1 ]; then
        echo "Add contact"
        add_contact
    elif [ $code == 2 ]; then
        echo "Update contact"
        update_contact
    elif [ $code == 3 ]; then
        echo "Search contact"
        search_contact
    elif [ $code == 4 ]; then
        echo "Delete contact"
        delete_contact
    elif [ $code == 252 ]; then
        echo "Linux Toolbox closed by user"
        exit
    fi

}

function search_contact (){

    echo "Search contact"

    search_field=$(yad --form --center --borders=10 --width=350 --height=300 --title="Linux Toolbox" --separator=' ' --text-align=center --text="<span><b><big><big>Search Contact</big></big></b></span>" \
    --field="":LBL \
    --field="" \
    --button="Search":0 \
    --button="Back":1 \ ) code=${?}

    code=${?}

    if [ $code == 1 ]; then
        echo "Back to home window"
    elif [ $code == 0 ]; then
        echo "Search query: $search_field"
        contact_find $search_field
    elif [ $code == 252 ]; then
        echo "Linux Toolbox closed by user"
        exit
    fi
}

function contact_find {
    echo "Find contact"
    search=$(grep -i $1 contact_list.log)
    find_field=$(yad --form --center --borders=10 --width=450 --height=300 --scroll --title="Linux Toolbox" --separator=' ' --text-align=center --text="<span><b><big><big>Search Contact</big></big></b></span>"\
    --field="<b>Search Query:</b> $1\n\n<b>Result:</b>":LBL \
    --field="$search":LBL \
    --button="Back":1 \ ) code=${?}

    code=${?}

    if [ $code == 1 ]; then
        echo "Back to home window"
    elif [ $code == 252 ]; then
        echo "Linux Toolbox closed by user"
        exit
    fi
}

function delete_contact(){

    delete_string=$(yad --form --center --borders=10 --width=350 --height=300 --title="Linux Toolbox" --separator='ㅤ' --text-align=center --text="<span><b><big><big>Delete Contact</big></big></b></span>" \
    --field="<b>Enter Phone Number:</b>":LBL \
    --field="" \
    --button="Delete":0 \
    --button="Back":1 \ ) code=${?}


    if [ $code == 1 ]; then
            echo "Back to home window"
    elif [ $code == 0 ]; then

        echo "Delete string: $delete_string"
        
        entry_phone=$(echo $delete_string | awk -F "ㅤ" '{print $2}')
        full_string=$(grep -i "$entry_phone" contact_list.log)

        delete_via_phone=$(echo $full_string | awk '{print $5}')

        echo "Delete row: $delete_via_phone"

        if [ ! -z "$delete_via_phone" ]; then
                if [ $entry_phone == $delete_via_phone ]; then
                
                    sed -i -e "/$delete_via_phone/d" contact_list.log
                    echo "Contact deleted"
                else
                    phone_number_not_found
                fi
        else
            phone_number_not_found
        fi
    fi
}
# contact window end

function contact(){
    
    while :
    do
        contact=$(yad --form --center --borders=10 --width=350 --height=300 --title="Linux Toolbox" --separator=' ' --text-align=center --text="<span><b><big><big>Manage Contacts</big></big></b></span>"\
        --field="":LBL \
        --field="<b>Add:</b> Add contact information. i.e: Name, Email Address, and Phone Number.":LBL \
        --field="<b>Update:</b> Update a specific contact.":LBL \
        --field="<b>View:</b> View the contact list after adding one or more contact to list.":LBL \
        --field="<b>Search:</b> Search a specific contact.":LBL \
        --field="<b>Delete:</b> Delete a specific contact.":LBL \
        --button="Add":1 \
        --button="Update":2 \
        --button="View":3 \
        --button="Search":4 \
        --button="Delete":5 \
        --button="Back":0 \ ) code=${?}

        code=${?}
        
        if [ $code == 0 ]; then
            echo "Contact window closed by user"
            ./linux_toolbox.sh
            exit
        elif [ $code == 1 ]; then
            echo "Add contact"
            add_contact
        elif [ $code == 2 ]; then
            echo "Update contact"
            update_contact
        elif [ $code == 3 ]; then
            echo "View contact"
            view_contact
        elif [ $code == 4 ]; then
            echo "Search contact"
            search_contact
        elif [ $code == 5 ]; then
            echo "Delete contact"
            delete_contact
        elif [ $code == 252 ]; then
            echo "Linux Toolbox closed by user"
            exit
        fi
    done
}

function home(){

    date=$(date +'%A %B %d, %Y')
    time=$(date +'%l:%M:%S %p')
    user=$(whoami)
    email=$(awk -F ":" '{print $1}' admin.log)
    # cmd: lscpu 
    #memory_usage=$(free | awk 'FNR==2 {print $3/(1024*1024),"/ "$2/(1024*1024)" GB"}')
    swap_usage=$(free | awk 'FNR==3 {print $3/(1024*1024),"/ "$2/(1024*1024)" GB"}')
    free_disk=$(df | awk 'FNR==4 {print $3/(1024*1024),"/ "$2/(1024*1024)" GB"}')

    total_memory=$(cat /proc/meminfo | awk 'FNR==1 {print $2/(1024*1024)}')
    availabe_memory=$(cat /proc/meminfo | awk 'FNR==3 {print $2/(1024*1024)}')
    memory_usage=$(echo "$total_memory - $availabe_memory" | bc)

    swap_usage=$(free | awk 'FNR==3 {print $3/(1024) "  MB","/ "$2/(1024*1024)" GB"}')
    disk_usage=$(df | awk 'FNR==4 {print $3/(1024*1024) " GB","/ "$2/(1024*1024)" GB"}')

    processor=$(cat /proc/cpuinfo | awk -F ": " 'FNR==5 {print $2}')
    cache_size=$(cat /proc/cpuinfo | awk -F ": " 'FNR==9 {print $2}')
    total_processor=$(cat /proc/cpuinfo | grep processor | wc -l)
    os_type=$(lscpu | awk 'FNR==2 {print $4}')
    os_support=$(lscpu | awk 'FNR==2 {print $3,$4}')
    cpu_speed=$(lscpu | awk 'FNR==16 {print $3}' | sed 's/\..*//')
    cpu_min_speed=$(lscpu | awk 'FNR==18 {print $4}' | sed 's/\..*//')
    cpu_max_speed=$(lscpu | awk 'FNR==17 {print $4}' | sed 's/\..*//')
    total_task=$(ps -e | wc -l)

    yad --title="Linux Toolbox" --center --borders=10 --width=450 --height=500 --text-align=center --separator="" --text="<span><b><big><big>System Info</big></big></b></span>" \
    --field="":LBL \
    --form \
    --field="<b><u>Basic Info</u></b>":LBL \
    --field="<b>Date:</b> $date":LBL \
    --field="<b>Time:</b> $time":LBL \
    --field="<b>Username:</b> $user":LBL \
    --field="<b>Email:</b> $email":LBL \
    --field="":LBL \
    --field="<b><u>Memory Info</u></b>":LBL \
    --field="<b>Memory Usage: </b>$memory_usage GB / $total_memory GB":LBL \
    --field="<b>Memory Available: </b>$availabe_memory GB":LBL \
    --field="<b>Swap Usage: </b>$swap_usage":LBL \
    --field="<b>Disk Usage: </b>$disk_usage":LBL \
    --field="":LBL \
    --field="<b><u>CPU Info</u></b>":LBL \
    --field="<b>Processor: </b>$processor × $total_processor":LBL \
    --field="<b>OS Type: </b>$os_type":LBL \
    --field="<b>OS Support: </b>$os_support":LBL \
    --field="<b>Cache Size: </b>$cache_size":LBL \
    --field="<b>CPU Speed: </b>$cpu_speed MHz":LBL \
    --field="<b>CPU Min Speed: </b>$cpu_min_speed MHz":LBL \
    --field="<b>CPU Max Speed: </b>$cpu_max_speed MHz":LBL \
    --field="<b>Total Task: </b>$total_task":LBL \
    --field="Calendar":FBTN "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "./calendar.sh" \
    --button="Notebook":2 \
    --button="Contact":3 \
    --button="Refresh":1 \
    --button="Log out":4 \
    --button="Exit":0
    code=${?}

    if [ $code == 0 ]; then
        echo "Linux Toolbox closed by user"
        exit
    elif [ $code == 1 ]; then
        echo "Refresh Date: $date $time"
    elif [ $code == 2 ]; then
        echo "Notebook window"
        notebook
    elif [ $code == 3 ]; then
        echo "Contact window"
        contact
    elif [ $code == 4 ]; then
        echo "Logged out"
        rm loggedin.log
        ./linux_toolbox.sh
        exit
    elif [ $code == 252 ]; then
        echo "Linux Toolbox closed by user"
        exit
    fi
}

export -f calendar home notebook view_all_note contact add_contact view_contact search_contact contact_find update_contact phone_number_not_found delete_contact
# bash functions end

if [ -f "$FILE" ]
then
    while :
    do
        
        if [ -f "$LOGGEDIN" ]
        then
            old_usr_pass=$(cat admin.log)
            log_usr_pass=$(cat loggedin.log)

            if [ $old_usr_pass == $log_usr_pass ]
            then
                # If the user has already logged in, redirected in home page
                echo "User already logged in"
                home
            else
                rm loggedin.log
                echo "User not logged in"
            fi
        else
            # login window start
            echo "Login window..."
            # login window
            login=$(yad --form --center --borders=10 --width=400 --height=300 --title="Linux Toolbox - Login" --separator=' ' \
                --button="Calendar":"bash -c calendar" \
                --button=Exit:1 \
                --button=Login:0 \
                --field="Email":\
                --field="Password":\') valid=${?}

            if [ $valid -eq 0 ]; then

                email=$(echo $login | awk '{print $1}') 
                password=$(echo $login | awk '{print $2}')

                echo "Email = $email"

            elif [ $valid -eq 1 ]; then
                echo "Linux Toolbox closed by user"
                exit
            fi
            # login window end

            echo -e "$email:$password" >> temp.log

            old_usr_pass=$(cat admin.log)
            crnt_usr_pass=$(cat temp.log)

            if [ $old_usr_pass == $crnt_usr_pass ]
            then
                rm temp.log
                echo "Login Successful"
                echo "$email:$password" >> loggedin.log
                # login success window start
                home
                # login success window end
            else
                rm temp.log
                # login failed window start
                echo "Login Failed!"
                echo "Wrong email or password"
                # login failed window end
            fi

        fi

    done

else
    # create admin.log file start
    echo "Signup window..."

    # signup window
    signup=$(yad --form --center --borders=10 --width=350 --height=300 --title="Linux Toolbox - Signup" --separator=' ' --text-align=center --text="<span><b><big><big>Signup</big></big></b></span>"\
        --field="":LBL \
        --button="Calendar":"bash -c calendar" \
        --button=Exit:1 \
        --button=Signup:0 \
        --field="Email":\
        --field="Password":\') valid=${?}

    if [ $valid -eq 0 ]; then

        email=$(echo $signup | awk '{print $1}') 
        password=$(echo $signup | awk '{print $2}')

        echo "Email = $email"
        # echo "Password = $password"

        echo "$email:$password" >> admin.log
        # create admin.log file end

        ./linux_toolbox.sh

    elif [ $valid -eq 1 ]; then
        echo "Linux Toolbox closed by user"
        exit
    fi
    
fi
Source Code - Calendar
echo "Calendar window"

yad --center --borders=10 --width=420 --title="Linux Toolbox - Calendar" --calendar --button=Back:0

if [ ${?} -eq 252 ]; then
    echo "Linux Toolbox closed by user"
    exit
fi

Chapter 3 Performance

Evaluation

3.1 Simulation

We have been able to meet the goals we set for ourselves in this project. Our code worked properly and was able to run Linux Toolbox. Also, the additional functions we added for user interaction all worked the way we initially expected. Each step along the way we tested our code to make sure everything was properly mapped.

3.2 Results and Discussions

After running Linux Toolbox, it will show the Sign-up window for the first time. Once the user has successfully signed up, the user will be shown a login window. And then the user needs to log in to use Linux Toolbox. Here users can use a blank email and password when signing up.

Sign-up window
Sign-in window

Figure-1: Sign-up and Login Window

After successful login, users will be able to view system information, add notebooks, and manage contact information.

System Information Window

Figure-2: System Information Window

Calendar Window

Figure-3: Calendar Window

Create Notebook Window

Figure-4: Create Notebook Window

Notebook Window

Figure-5: Notebook Window

Manage Contacts Window

Figure-6: Manage Contacts Window

Update Contacts Window
Search Contacts Window

Figure-7: Update and Search Contacts Window

Search Contacts Window

Figure-8: Search Contacts Window

Figure-9: Contact List Window

3.3 Analysis and Outcome

Our goal was to see the monthly calendar, add notes, manage contact, and view system information. In this project, our code worked properly and was able to successfully run Linux Toolbox. Also, the additional functions we added for user interaction all worked the way we initially expected. Each step along the way we tested our code to make sure everything was properly mapped.

3.4 Limitations

Although Linux Toolbox runs properly, there is some limitation for this application.

  1. In the Manage Contact part, the user can add multiple strings like a name but can’t update multiple strings as a name.
  2. The user can’t modify/update, search and delete Notebook.
  3. There may be other bugs in the Linux toolbox.

Chapter 4

Conclusion

4.1 Introduction

In this project, we gained a lot of knowledge and experience working with the Linux operating system. We learned how to work Bash language. Also, we learned more about the basic principles of Linux application. Our team members have made improvements in various areas such as developing a structured information pathway for all types of software and understanding more about state machines. Although this is a YAD base project, it was a great experience in developing software. The principle behind constructing a Linux Toolbox is to effectively retrieve and implement any information. All the information related to a particular people can be linked and archived only to be retrieved later when they are required most.

4.2 Practical Implications

Linux Toolbox is one of the useful applications used to show the system information. This type of software can be used to note down user information and create contact and also manage the contact information.

4.3 Scope of Future Work

In the future, we have planned to add more features for Linux Toolbox. For example, users can manage background applications such as kill or end process for Linux/GNU operating system. It would be handy for all Linux users. We have a plan to increase the limit of features. And we will upgrade it with many more facilities in the future.

Change Theme
X