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

  • SEARCH
  • Home
  • 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 6796987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:33:13+00:00 2026-05-26T18:33:13+00:00

I have developed a menu in bash with some options(only backup will be included

  • 0

I have developed a menu in bash with some options(only backup will be included here), one of these options is to backup, however whenever I close the shell, the backup command runs and makes a backup by it self.

I tried adding break and exit before ;; but it just stops the whole script.

#!/bin/bash

menu=$(echo "Choose operation [1]Hi [2]Backup")

echo "$menu"

while [ $# == 0 ]; do
  read options

  case "$options" in
    1 )  
    echo "HI"  
    echo "$menu"
  ;;

  2 )
    dir="/home/backups"
    bckup="$(date +%d%b%Y_%H%M)"
    mkdir $dir/$bckup
    cat /etc/passwd >> $dir/$bckup/"DB$bckup".txt

    dialog --title "DATABASE" --msgbox " Ready to backup user database. \n
    press <Enter> to start or <Esc> to cancel." 10 50

    # Return status of non-zero indicates cancel
    if [ "$?" != "0" ]
    then
        dialog --title "BACKUP" --msgbox " Backup was canceled at your request." 10 50
    else
        dialog --title "BACKUP" --infobox " Backup in process..." 10 50 ; sleep 1

        tar czf $dir/$bckup.tgz -C $dir/$bckup . >|/tmp/ERRORS$$ 2>&1

      # zero status indicates backup was successful
      if [ "$?" = "0" ]
      then
        dialog --title "BACKUP" --msgbox " Backup completed successfully." 10 50
      else
        dialog --title "BACKUP" --msgbox "Backup failed Press <Enter> to see error log." 10 50
       dialog --title "Error Log" --textbox /tmp/ERRORS$$ 22 72
      fi
    fi
    rm -r -f $dir/$bckup
    rm -f /tmp/ERRORS$$
    clear
    echo "$menu"
  ;;

  *)
    echo "Invalid, Please choose a valid option"
  ;;
esac
done
exit
  • 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-26T18:33:14+00:00Added an answer on May 26, 2026 at 6:33 pm

    What happens when you type EOF to:

    read options
    

    You don’t test whether that worked. It is likely that options contains the previous value, and that you previously tried backup. If the diagnosis is correct, then the fix is pretty simple:

    if read options
    then
        ...the script you currently have interpreting $options
    else break  # The while loop ... which has an interesting (read 'unusual') condition.
    fi
    

    You could (should?) provide an explicit exit option too, but this should protect you from unexpected backups after you type Control-D or whatever it is you use as an EOF indication to the terminal.

    A slightly more radical reorganization uses:

    if [ $# -eq 0 ]
    then
        while read options
        do
            ...the script you currently have interpreting $options
        done
    fi
    

    or:

    if [ $# -ne 0 ]
    then exit 1
    fi
    
    while read options
    do
        ...the script you currently have interpreting $options
    done
    

    Please tell me what’s the difference between while [ $# == 0 ]; and [ $# -ne 0 ] and -eq.

    It is a question of shell semantics.

    • while [ $# == 0 ] does a string equality comparison of the number of arguments $# against 0.
    • while [ $# != 0 ] does a string inequality comparison of the number of arguments and 0.
    • while [ $# -ne 0 ] does a numeric inequality comparison of the number of arguments and 0.
    • while [ $# -eq 0 ] does a numeric equality comparison of the number of arguments and 0.

    Note that this is the converse of the Perl convention, where:

    • $x == 0 is numeric equality
    • $x != 0 is numeric inequality
    • $x eq 0 is string equality
    • $x ne 0 is string inequality

    In all pairs of cases, the result is the same when the comparisons are (in)equality and the values are expressed conventionally (using the minimum number of digits and expressed as an integer). The numeric versus string comparison matters for ordering comparisons (>, <, >=, <=).

    Your original code loops on while [ $# == 0 ] (while there are no positional arguments). If there are any arguments (so $# != 0 or $# > 0, since the number of arguments is never negative), the loop is never entered.

    The first of my rewrites checks that there are no arguments before entering the while read options loop.

    The second of my rewrites exits early if there are any arguments and so does not enter the while read options loop at all when arguments are present. They are equivalent except that the first rewrite has the body of the loop nested one level more deeply.

    Note that none of the code shown ever changes the number of positional parameters (number of arguments). You’d do that with set -- arg1 arg2 ... if you really wanted to, but you probably don’t.

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

Sidebar

Related Questions

I have developed some classes with similar behavior, they all implement the same interface.
I have developed some custom DAO-like classes to meet some very specialized requirements for
We have developed a website that uses MVC, C#, and jQuery. In one of
I have developed an android app that mainly targets smartphones. However in tablet emulator
i had developed joomla site, Yogamother.com . In this site top menu have drop
I have developed some app and want to add some manual for it. I
I have recently developed an Eclipse plugin, however the plugin does not seem to
I have developed a C# application - I want to now add a menu
I have developed a new custom extension for magento admin section. Main menu option
i have an ie problem for some reason my tabbed menu shows the opposite

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.