lack of understanding > annoying error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting lack of understanding > annoying error
# 22  
Old 10-28-2009
A common issue with programmers is that they don't test their code in pieces, to verify they know how things are going to behave. To troubleshoot the shell script, break it apart, one component, each command or a line at a time, until you find the problem. If you type it on the command line, it can store in your local shell, you can test the variables, and the output of the commands, one at a time to determine the proper expected output, one command at a time.

Our first issue, why source the script into your current shell? This is additional overhead that doesn't appear to provide any benefit.

Set the execution bit and start testing. Use "set +x" and "set -x", as often as needed, while debugging to see the code as it is presented for execution, see the values as the value tests are performed, etc.

I would ensure your code exits with an error code, if it is not going to run, instead of simply dropping out. This ensures some feedback from cron, or other scheduling tool, the ability to automate proper responses to script failure.

Instead of setting the variable work, when you are done with the first exception, exit 1 (or some other values). This eliminates the need for an "if" test to start your "Main Module" and simplifies your code. Each new reason for exiting may need a different exit code, to determine proper next steps, for the calling program or shell to interpret and respond apporpriately.

grep *tar* ... will likely return more than you expect. Use of wildcards in with grep search strings requires a bit of finesse. You might want to read the O'reilly book on regex sometime. Simply use grep tar, instead.

The use of
Code:
    toBackUp="$toBackUpDir/*" #PATH TO BE BACKED UP

is bad form, if you must gather your list of files, do it later, on the command line, not as part of a variable. Every time you use this variable, it will return a space separated list of files, or may cause additional problems, when the list of files exceeds the command line lor variable length, which is usually under 1024 characters.

I concur with jzacsh tom_cmu, regarding the wrap of the commented code. The second line also needs the leading #. This is the first reason the code stops before executing anything in the "main module". This would looks something like:
Code:
        #OLD WAY: #fileName=`date | sed -n s/ /_/g p | sed -n s/^/Backup_/p | sed -n s/$/\.tgz/p | \
# sed -n s/^/$landingDir/p`  #NAME OF BACKUP FILE

I highly recommend the use of curly braces around each variable reference.
My versions of sed, on Solaris 8, 9 and 10, require quotes around the command to work. We generally use [date | sed "s/ /_/

I concur with TonyFullerMalv's comments about quotes around variables when testing, expecially if you expect an empty string for comparison.

The cause of all of your headaches comes from your sed in
Code:
# MYSQL BACKUP
                   sql_bName=`date | sed -n s/ /_/g p | sed -n s/^/SqlBackup_`

which should be modeled like you do in the previous instance
Code:
# MYSQL BACKUP
                   sql_bName=`date | sed -n s/ /_/g p | sed -n s/^/SqlBackup_/p`

I would use a different construct for ./{dirname}. Some prefix, or fully qualified directory name would be better, or pass the starting directory to the script on the command line.

z is not a common option for tar, usually you'll want more portable code to run in other environments, beyond the one you currently use. to be more portable, you will want to use something like:
Code:
tar cvf - ${list} | gzip > ${filename}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I get out of the annoying > in bash???

Occasionally I make a mistake in my shell that results in there being a > for the prompt instead of the normal $. Today I accidentally left off a " in a sed command, sed s/\"//g" infile > outfile and then I get $ sed s/\"//g" infile > outfile > > I have never figured out how to get... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. IP Networking

Lack of IP Connectivity

Hi Can any one please help identify the issue in scenario 2: Connectivity Diagram: 1) Distribution Switch----Int_Switch----LabSwitch(Fa1/0)----Terminal Ser 2) Distribution Swtich----Int_Swtich----LabSwitch(Fa2/0)----3640 Router ---all links are access links Distribution Switch... (0 Replies)
Discussion started by: sureshcisco
0 Replies

3. Shell Programming and Scripting

Syntax error, not understanding the issue?

Close please. Refer to following thread: Sub Menu issues (2 Replies)
Discussion started by: Banned
2 Replies

4. UNIX for Advanced & Expert Users

Annoying in VI editor

Dear all, I try to search " ( double quote ) in a file using vi editor, I gave in the command mode /" it supposed to take to me to all the occurnces of " instead in some places it is taking me to different character.! It happens with some other characters in that file.... can you... (5 Replies)
Discussion started by: shahnazurs
5 Replies

5. Shell Programming and Scripting

Need help understanding perl script error

I solicited this site earlier this week and got a good answer for a perl Script so I made this script from what understood from the answers But now I have a bug and I'm stump. It doesn't parse correctly the Output it stays on the first line My $f2 and reprints in a endless loop I'm sure there... (3 Replies)
Discussion started by: Ex-Capsa
3 Replies

6. Shell Programming and Scripting

Help understanding syntax error Issue

Hi i as you may already know i am creating a menu driven program. I have chosen to take the approach of implementing each interface individually, after adding another interface and attempting to run the program i am faced with the following error: ./Assigntest: line 32: syntax error near... (6 Replies)
Discussion started by: warlock129
6 Replies

7. Post Here to Contact Site Administrators and Moderators

Annoying tooltips

Hi Is there any way to turn off the (often ridiculously big) tooltips that are displayed when hovering over a topic in a topic list? It's driving me nuts. Thx. J (1 Reply)
Discussion started by: jgrogan
1 Replies

8. Shell Programming and Scripting

Very ANNOYING Problem - Please Help

Hey Guys I have an extremely annoying problem with regular expressions! At this point i believe the command 'read' is causing the problem due to the carriage return it places once its done. I have an continuous loop until the input is correct: (After initial read statement) while ... (7 Replies)
Discussion started by: shadow0001
7 Replies

9. UNIX for Dummies Questions & Answers

a very annoying problem

hi i got fbsd here,when i try to start my X server as an user I got hte following error. Fatal server error: xf86OpenConsole: Server must be running with root permissions You should be usig Xwrapper to start the server or xdm. We strongly advise against making the server SUID root! But... (2 Replies)
Discussion started by: Stormpie
2 Replies
Login or Register to Ask a Question