English 中文(简体)
Bash Shell Scripting Errors: ./myDemo: 56: Syntax error: Unterminated quoted string
原标题:

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?

最佳回答

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.

问题回答

Two problems in the A) directory listing section.

  1. The -o conjunction doesn t work like you think it does. Should be:

                if [ "$directory" = "q" -o "$directory" = "Q" ]
    
  2. Your outer "if" needs an "else" to handle the case when the directory given really is a directory right off the bat.

The B) backup section has the same two problems. Fix those, and both command options will work.

You ve quoted the $file variable in most places, but in the cp command you don t. It should be:

cp "$file" "$file.bkup"

Some of your echo commands have "c" at the end. I think that s specific to csh. Bash will just echo the characters "" and "c" literally.

Your statement while $TRUE works by virtue of the variable being null or unset. If it gets set to some value, it will try to execute the contents as a command. If you want to do that type of infinite loop, it s typically done in Bash like this:

while true

where true is a shell builtin. Or:

while :

where the colon is a no-op that returns true. Of course there are other ways to accomplish the same thing.

In the l|L) case you probably want to do either:

ls

or

ls $PWD

the way you have it now, it s going to try to list the entry for a file named "pwd".

Both vim and nano can do syntax highlighting for Bash. If they are not already set up in ~/.nanorc and ~/.vimrc you can do these:

for nano:

nano -Y sh scriptname

For vim:

:syntax on
:set filetype=sh

1) you don t to use so many echoes.. to create a menu system, you can use cat here-document,eg

cat <<EOF
-------------------------------------------------------
Select one of the following options:
d or D) Display today s date and time
l or L) List the contents of the present working directory
w or W) See who is logged in
p or P) Print the present working directory
a or A) List the contents of a specified directory
b or B) Create a backup copy of an ordinary file
q or Q) Quit this program
--------------------------------------------------------
EOF

or even this will do

echo "-------------------------------------------------------
Select one of the following options:
d or D) Display today s date and time
l or L) List the contents of the present working directory
w or W) See who is logged in
p or P) Print the present working directory
a or A) List the contents of a specified directory
b or B) Create a backup copy of an ordinary file
q or Q) Quit this program
--------------------------------------------------------"

2) when asking use to choose an option, you can use read with -p, eg

read -p "Enter your option and hit <Enter>: " choice

3) printf is more portable than echo, therefore you should use it whenever possible.





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签