Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 510831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:09:14+00:00 2026-05-13T07:09:14+00:00

Could someone take a look at this code and find out what’s wrong with

  • 0

Could someone take a look at this code and find out what’s wrong with it?

#!/bin/sh
while :
do
    echo " Select one of the following options:"
    echo " d or D) Display today's date and time"
    echo " l or L) List the contents of the present working directory"
    echo " w or W) See who is logged in"
    echo " p or P) Print the present working directory"
    echo " a or A) List the contents of a specified directory"
    echo " b or B) Create a backup copy of an ordinary file"
    echo " q or Q) Quit this program"
    echo " Enter your option and hit <Enter>: \c"
    read option 
    case "$option" in
        d|D) date
             ;;
        l|L) ls $PWD
             ;;
        w|w) who
                 ;;
        p|P) pwd
             ;;
        a|A) echo "Please specify the directory and hit <Enter>: \c"
             read directory
                    if [ "$directory = "q" -o "Q" ]
                then
                    exit 0
                fi

                while [ ! -d "$directory" ]
                do
                        echo "Usage: "$directory" must be a directory."
                    echo "Re-enter the directory and hit <Enter>: \c"
                    read directory

                        if [ "$directory" = "q" -o "Q" ]
                        then    
                            exit 0

                        fi

                done
                    printf ls "$directory"

            ;;  
            b|B) echo "Please specify the ordinary file for backup and hit <Enter>: \c"
             read file
                if [ "$file" = "q" -o "Q" ]
                then
                    exit 0
                fi     

                while [ ! -f "$file" ]
                do
                    echo "Usage: \"$file\" must be an ordinary file."
                    echo "Re-enter the ordinary file for backup and hit <Enter>: \c"
                    read file
                        if [ "$file" = "q" -o "Q" ]
                        then
                            exit 0
                        fi              
                done
                    cp "$file" "$file.bkup"
                 ;;

        q|Q) exit 0
             ;;

    esac
    echo
done
exit 0

There are some syntax errors that I can’t figure out. However I should note that on this unix system echo -e doesn’t work (don’t ask me why I don’t know and I don’t have any sort of permissions to change it and even if I wouldn’t be allowed to)

Bash Shell Scripting Error: “./myDemo ./myDemo: line 62: syntax error near unexpected token done' ./myDemo: line 62:” [Edited]

EDIT: I fixed the while statement error, however now when I run the
script some things still aren’t
working correctly.

  1. It seems that in the b|B) switch statement

    cp $file $file.bkup doesn’t
    actually copy the file to file.bkup ?

  2. In the a|A) switch statement

ls “$directory” doesn’t print the directory listing for the user to see
?

#!/bin/bash
while $TRUE
do
        echo " Select one of the following options:"
        echo " d or D) Display today's date and time"
        echo " l or L) List the contents of the present working directory"
        echo " w or W) See who is logged in"
        echo " p or P) Print the present working directory"
        echo " a or A) List the contents of a specified directory"
        echo " b or B) Create a backup copy of an ordinary file"
        echo " q or Q) Quit this program"
        echo " Enter your option and hit <Enter>: \c"
        read option
        case "$option" in
                d|D) date
                     ;;
                l|L) ls pwd
                     ;;
                w|w) who
                     ;;
                p|P) pwd
                     ;;
                a|A) echo "Please specify the directory and hit <Enter>: \c"
                     read directory
                        if [ ! -d "$directory"  ]
                        then
                                while [ ! -d "$directory" ]
                                do
                                        echo "Usage: "$directory" must be a directory."
                                        echo "Specify the directory and hit <Enter>: \c"
                                        read directory

                                        if [ "$directory" = "q" -o "Q" ]
                                        then
                                        exit 0

                                        elif [ -d "$directory" ]
                                        then
                                                ls "$directory"

                                        else
                                        continue
                                        fi
                                done
                        fi
                        ;;
                b|B) echo "Specify the ordinary file for backup and hit <Enter>: \c"
                     read file
                        if [ ! -f "$file" ]
                         then
                                while [ ! -f "$file" ]
                                do 
                                        echo "Usage: "$file" must be an ordinary file."
                                        echo "Specify the ordinary file for backup and hit <Enter>: \c"
                                        read file
                                        if [ "$file" = "q" -o "Q" ]
then
                                        exit 0
                                        elif [ -f "$file" ]
                                        then
                                        cp $file $file.bkup
                                        fi
                                done
                        fi
                        ;;

                q|Q) exit 0
                     ;;

        esac
        echo
done
exit 0

Another thing… is there an editor that I can use to auto-parse code? I.e something similar to NetBeans?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:09:14+00:00Added an answer on May 13, 2026 at 7:09 am

    You are missing a do after the second while. (The ‘B’ case; compare that against the ‘A’ case above it.)

    I use gvim which will syntax highlight shell scripts, but I think you need to ask about editors as a separate question.


    As for your modified question:
    Your logic is broken in both the A and B cases: you need to pull the backup logic out of your if/while nesting… the if isn’t actually doing anything for you. Also, be sure to quote all your filenames so that spaces don’t break your script. Escape your nested quotes. I believe you need a -e on the echo statements that use \c.

    So do something more like:

    b|B) echo -e "Specify the ordinary file for backup and hit <Enter>: \c"
        read file
        while [ ! -f "$file" ]
        do 
            echo "Usage: \"$file\" must be an ordinary file."
            echo -e "Specify the ordinary file for backup and hit <Enter>: \c"
            read file
            if [ "$file" = "q" -o "$file" = "Q" ]
            then
                exit 0
            fi
        done
        cp "$file" "$file.bkup"
        ;;
    

    You’ll need to do the same kind of change for the A case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 398k
  • Answers 398k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, the closest I can think, without technically passing the… May 15, 2026 at 3:43 am
  • Editorial Team
    Editorial Team added an answer That's just a guess, but I think it might be… May 15, 2026 at 3:43 am
  • Editorial Team
    Editorial Team added an answer The ideal solution would be to rename your module to… May 15, 2026 at 3:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.