LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Some annoying output - something small, just cannot see what? (https://www.linuxquestions.org/questions/linux-newbie-8/some-annoying-output-something-small-just-cannot-see-what-910146/)

Batistuta_g_2000 10-25-2011 06:11 PM

Some annoying output - something small, just cannot see what?
 
removed

jhwilliams 10-25-2011 06:39 PM

Hi again Batistuta,

Your script has some structural problems. Here is my interpretation of what I think you're getting at:

Code:

#!/bin/bash
# ... blah blah ...
# ... find command here ...
retval=$?

if [ $retval -eq 0 ]; then
    echo "Do backup stuff"
else
    perror $retval
fi

exit $retval


unSpawn 10-25-2011 06:58 PM

...even worse using 'find -L */"$filename";' definitely isn't the right way: either make the user supply the full path, a search path or search the local directory (use 'optargs'?). Then $filename isn't propagated down the script so 'echo "file "$filename" found;' doesn't even show the actual file (or files!) found and it doesn't protect against spaces and such. And usually you have an idea of which files to back up. I mean you don't go backing up random files.

Code:

function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq
http://wooledge.org/mywiki/BashPitfalls"; }


Batistuta_g_2000 10-26-2011 05:40 AM

removed

jhwilliams 10-26-2011 03:33 PM

Batistuta,

It's coming along, keep up the effort! A few things.

Your if statements are still not correct, but they're closer. Please see my other post.

Here's a marked up version, discussing a few of the issues:

Code:

#!/bin/bash

# This find command looks for a file named retval.
find retval

# So this is probably going to set the variable retval to 1, not 0 (true.)
retval=$?

if [ $retval -eq 0 ]; then
    echo "Do backup stuff [y/n]"
    read yn
      # the second 'if' is out of flow.
      # You should at least quote (with ") your variables.
      if [$yn = y] || if [$Y = Y]
        # "do" is for loops, not for the if statement. See the man page.
        do "Blah"
      # Why not just use an else statement?
      # This is the correct way to use || with two expression clauses, though.
      elif [$yn != y] || [$yn != Y]
        # see above comment about do.
          do Exit
      fi
else
    # You're missing an echo call.
    "not found exiting"

fi

exit $retval


Another free tip: try debugging your scripts by adding set -x to the top.

Here's another revision that should do what you want.

Code:

#!/bin/bash

set -x

# Assume you run some find command that returns 0, here we mock up with the 'true' binary.
true
retval=$?

if [ $retval -eq 0 ]; then
    echo "Do backup stuff [y/n]"
    read yn
    do_backup=$(echo "$yn" | grep -c '^[yY]$')

    if [ $do_backup -eq 1 ]; then
        echo "Doing backup stuff..."
        # Do the backup stuff.
    else
        echo "Okay, bye."
    fi
else 
    echo "Couldn't find any files."
fi

exit $retval


Batistuta_g_2000 10-27-2011 01:58 PM

removed

Batistuta_g_2000 10-28-2011 08:27 PM

removed


All times are GMT -5. The time now is 11:32 AM.