Shell Script Woes for UNIX student, help!!! :)

Shell Script Woes for UNIX student, help!!! :)

Post by Bria » Sun, 03 Nov 2002 03:31:27



Can someone debug this for me and email me back please, I'm stuck...Need
your expertise please. Thank you in advance.

#========================
# Simple Backup Script
#========================

clear
mkdir -p ~/backup
LIST=`ls *`
for name in $LIST
echo -e "Do you want to backup $name ? \n"
read reply
do
if [ "$reply" = "y" ]
then
cp -f $name ~/backup
echo "File saved!"
echo -e "$name was backed up at: `date`" >> backup.log
elif [ "$reply" = "Y" ]
then
cp -f $name ~/backup
echo "File saved!"
else
echo "No file saved."
fi
done
exit 0

 
 
 

Shell Script Woes for UNIX student, help!!! :)

Post by John Gordon,217-352-6511x7418,CEERD-CF- » Sun, 03 Nov 2002 03:51:09



> Can someone debug this for me and email me back please, I'm stuck...Need
> your expertise please. Thank you in advance.

it would help if you told us what it's doing wrong.  also, what
shell are you using?

John Gordon
---
"She even named one city after Robert, her ex-boyfriend, just to annoy
me.  I have it in a saved game on my laptop.  Every now and then I boot
it up just to let Robertville starve itself off the map."  -- Tom Chick

 
 
 

Shell Script Woes for UNIX student, help!!! :)

Post by Chris F.A. Johnso » Sun, 03 Nov 2002 04:30:38



> Can someone debug this for me and email me back please, I'm stuck...Need
> your expertise please. Thank you in advance.

> #========================
> # Simple Backup Script
> #========================

> clear
> mkdir -p ~/backup
> LIST=`ls *`
> for name in $LIST

  do

Quote:> echo -e "Do you want to backup $name ? \n"
> read reply
> do

  do not

Quote:> if [ "$reply" = "y" ]
> then
> cp -f $name ~/backup
> echo "File saved!"
> echo -e "$name was backed up at: `date`" >> backup.log
> elif [ "$reply" = "Y" ]
> then
> cp -f $name ~/backup
> echo "File saved!"
> else
> echo "No file saved."
> fi
> done
> exit 0

    Apart from having "do" on the wrong line, your script should work,
    though you should enclose $name in quotation marks in case there
    are any file names containing spaces.

    Here is a more efficient version:

        mkdir -p ~/backup
        for name in *
        do
          echo -e "Do you want to backup $name ? \n" ## see below
          read reply

          case $reply in
              [yY]*)
                  cp -f "$name" ~/backup
                  echo "File saved!"
                  echo -e "$name was backed up at: `date`" >> backup.log
                  ;;
              *) echo "$name not saved." ;;
          esac
        done

    In bash, I'd use this for the prompt and read lines:

       read -n1 -p "Do you want to backup $name ? " reply

    Then the user doesn't need to press RETURN.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Shell Script Woes for UNIX student, help!!! :)

Post by Barry Margoli » Sun, 03 Nov 2002 04:27:20



>clear
>mkdir -p ~/backup
>LIST=`ls *`
>for name in $LIST
>echo -e "Do you want to backup $name ? \n"
>read reply
>do

This "do" line needs to be immediately after the "for" line.

Quote:>if [ "$reply" = "y" ]
>then
>cp -f $name ~/backup
>echo "File saved!"
>echo -e "$name was backed up at: `date`" >> backup.log
>elif [ "$reply" = "Y" ]
>then
>cp -f $name ~/backup
>echo "File saved!"

It seems strange that you don't add a line to backup.log in this case.

Quote:>else
>echo "No file saved."
>fi
>done
>exit 0

The mis-placed "do" appears to be the only serious error.

You should indent your code to make it easier for others to read,
*especially* when you're posting it for comments.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Shell Script Woes for UNIX student, help!!! :)

Post by Dan Merc » Sun, 03 Nov 2002 04:32:53




> Can someone debug this for me and email me back please, I'm stuck...Need
> your expertise please. Thank you in advance.

> #========================
> # Simple Backup Script
> #========================

> clear
> mkdir -p ~/backup
> LIST=`ls *`
> for name in $LIST                              ^>>>BELONGS after HERE
> echo -e "Do you want to backup $name ? \n"     ^
> read reply                                     ^
> do                                    <<<< This^
> if [ "$reply" = "y" ]
> then
> cp -f $name ~/backup
> echo "File saved!"
> echo -e "$name was backed up at: `date`" >> backup.log
> elif [ "$reply" = "Y" ]
> then
> cp -f $name ~/backup
> echo "File saved!"
> else
> echo "No file saved."
> fi
> done
> exit 0

--
Dan Mercer

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

1. Shell Script Help (C-Shell Script)

In my class we had to write a C program and now we have to write a shell
script to do the exact same thing. I am not sure where to get started using
tr and sed to accomplish this assignment. Here is the assignmnet...

--------------------------------------------------------------
(1) remove all non-printing characters (character codes 1 through 31,
   and 127) and replace them with the same sort of codes the vi
   editor uses.  For example, the bell character (character code 7)
   is replaced by the two printable characters "^" and "G" (G is the seventh
   letter of the alphabet).  Appendix I of Foster lists the ASCII character
   set, and it shows all the proper two-character sequences that should
   be used for the non-printing characters.  There are two special cases:
   Replace the delete character by "^?".  Newlines should be replaced by a
   dollar sign ("$") and then an actual newline.
   **you may ignore NUL (character code 0)**

(2) delete all non-ASCII characters (those with character codes 128 through
255).

(3) truncate all output lines that would otherwise extend past column 72.
Begin
   reading the data on the next line after ignoring the rest of the
character past column 72.

Make sure it reads from stdin and prints to stdout.  It should be comprised
of standard UNIX utilities (that is, it should not invoke any specialized C
programs
that you may consider writing). You MUST make use of pipes rather than using
files to store intermediate results.  Don't make this assignment
unnecessarily hard: the whole program can be done in ONE line (not counting
all the documentation lines).
--------------------------------------------------------------

I was thinking that (1) could be accomplished by using a tr line like ( tr
'\001-\011' '\136') but that replaces some of the non printing with the ^,
how would i get the letter in there and would I have to have a bunch of
those tr commands piped together just to accomplish (1)?

Some help on this would really really be appreciated as its due on Monday
and I've hit a brick wall on this one.

-mogrefy

2. Read-Copy Update 2.5.30

3. ? Unix Shell Syntax to log out user from a shell script ?

4. Bootable SPARCLinux CD

5. unix shell commands within ftp shell script

6. Problems to set up router

7. . sed script unix shell script ispell

8. AIX 5.1, VisualAge for C++ 5.0.2.2?

9. calling CGI scripts from UNIX shell scripts->html

10. UNIX Shell scripts... Help needed

11. UNIX Shell script.... help needed

12. Help - UNIX Shell script problem

13. Help W/Unix Shell Script - File Splitting Based on Text String