Bad Number Error In Shell Script
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or
Bad Variable Name Error Shell Script
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss shell script bad interpreter error Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only redirect error in shell script takes a minute: Sign up Shell script programming : “bad number” error up vote 1 down vote favorite I am writing a simple shell script and I am having a strange error about "bad number". Here is my code : status=0
How To Check Error In Shell Script
maxRetries=3 retryCount=1 while [[ status == 0 ]] || [[ retryCount -le maxRetries ]] do .... retryCount=$((retryCount+1)) done As far as I see, I have properly declared maxRetries and retryCount as integers, so I don't see why it complains about bad number on the while statement. Anyone have an idea? linux shell share|improve this question asked Oct 4 '13 at 0:40 Mickaël C. Guimaraes 7118 add a comment| 1 Answer 1 active oldest votes up vote 2 down vote accepted status,
Shell Script Error Code
retryCount and maxRetries are strings, not numbers. You want to expand those parameters with the $ sigil. Alternatively, you could use arithmetic expressions, which do not require the sigil. while (( status == 0 || retryCount < maxRetries )) share|improve this answer answered Oct 4 '13 at 0:41 kojiro 40.8k964111 status, is also missing a $ –sampson-chen Oct 4 '13 at 0:43 Allright, now it's working. So simple... Thank you! –Mickaël C. Guimaraes Oct 4 '13 at 0:43 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged linux shell or ask your own question. asked 2 years ago viewed 2726 times active 2 years ago Related 2103Check if a directory exists in a shell script721How do I prompt for input in a Linux shell script?389Why doesn't “cd” work in a bash shell script?225Get program execution time in the shell975Check if a program exists from a Bash script239Aborting a shell script if any command returns a non-zero value?351Shell command to tar directory excluding certain files/folders337How can I write a here doc to a file in Bash script?0Display numbers shell scripting6Debugging she
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta shell script error code 1 Discuss the workings and policies of this site About Us Learn
Shell Script Error Exit
more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us shell script error command not found Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like http://stackoverflow.com/questions/19171362/shell-script-programming-bad-number-error you, helping each other. Join them; it only takes a minute: Sign up [: : bad number on the bash script up vote 1 down vote favorite This is my bash script: #!/usr/local/bin/bash -x touch /usr/local/p touch /usr/local/rec DATA_FULL=`date +%Y.%m.%d.%H` CHECK=`netstat -an | grep ESTAB | egrep '(13001|13002|13003|13004|13061|13099|16001|16002|16003|16004|16061|16099|18001|18002|18003|18004|18061|18099|20001|20002|20003|20004|20061|20099|13000|16000|18000|20000)' | awk '{ print $5 }' | sort -u http://stackoverflow.com/questions/8941874/bad-number-on-the-bash-script | wc -l` netstat -an | grep ESTAB | egrep '(13001|13002|13003|13004|13061|13099|16001|16002|16003|16004|16061|16099|18001|18002|18003|18004|18061|18099|20001|20002|20003|20004|20061|20099|13000|16000|18000|20000)' | awk '{ print $5 }' | sort -u | wc -l > /usr/local/www/p STAT=`cat /usr/local/www/rec` if [ "$CHECK" -gt "$STAT" ]; then echo $CHECK"\n"$DATA_FULL > /usr/local/p fi Ofcourse I've runned chmod +x script.sh and then sh script.sh, then I receive the following message: [: : bad number. Why does it happends? bash unix freebsd share|improve this question asked Jan 20 '12 at 13:03 Cyclone 3,647175193 Does /usr/local/www/rec contains any number? –shiplu.mokadd.im Jan 20 '12 at 13:19 add a comment| 4 Answers 4 active oldest votes up vote 1 down vote accepted If these are integer for sure, use this syntax, if [ "0$(echo $CHECK|tr -d ' ')" -gt "0$(echo $STAT|tr -d ' ')" ]; A simple hack. Only works if $STAT is always either empty or positive number. share|improve this answer edited Jan 20 '12 at 14:23 answered Jan 20 '12 at 13:25 shiplu.mokadd.im 36.5k571142 Still: [: 0 1: bad number &
Display results as threads More... Useful Searches Recent Posts Menu Forums Forums Quick Links Search Forums Recent Posts Menu Log in Sign up AnandTech Forums: Technology, Hardware, Software, and Deals Forums > Software > Programming https://forums.anandtech.com/threads/more-unix-shell-scripting-help-needed-bad-number-ksh.2173555/ > More UNIX Shell Scripting Help Needed "Bad Number" (KSH) Discussion in 'Programming' started http://linuxcommand.org/wss0150.php by Saint Nick, Jun 22, 2011. Saint Nick Lifer Joined: Jan 21, 2005 Messages: 17,723 Likes Received: 0 Here is my code Code: #!/usr/bin/ksh clear file=/home/x12960/apedi.dat line_nbr=0 next_line_nbr=`expr ${line_nbr} + 1` file_lines=`wc -l ${file}` outfile=new_apedi.dat #echo $file #echo $line_nbr #echo $next_line_nbr #echo $file_lines #echo $outfile while [[ $line_count -lt $file_lines ]] do line=`sed -n "${line_nbr}p" $file` next_line=`sed -n "${next_line_nbr}p" shell script $file` line_char=`echo $line | cut -c1-1` next_line_char=`echo $next_line | cut -c1-1` if [[ $line_char = "H" ]] ; then if [[ $line_char -eq $next_line_char ]] ; then echo $next_line >> $outfile fi else echo $line >> $outfile fi if [[ $next_line_nbr -eq $file_lines ]] ; then line_nbr=`expr $line_nbr + 1` next_line_nbr=$next_line_nbr else line_nbr=`expr $line_nbr + 1` next_line_nbr=`echo ${line_nbr}` fi done But, when I run it, I am getting the following error: Code: ./apedi.scr[9]: shell script error 22 /home/x12960/apedi.dat: bad number In my file, line 9 is outfile=new_apedi.dat. I'd like to add that when I un-comment my echo statements, the same error shows up, but states it is at line 15 rather than line 9. Also, there are 22 lines in my test file. Thanks for the help! #1 Saint Nick, Jun 22, 2011 Last edited: Jun 22, 2011 Saint Nick Lifer Joined: Jan 21, 2005 Messages: 17,723 Likes Received: 0 Turns out that wc also returns the name of the file in the count...how can I get rid of that? There doesn't seem to be a command line option for it... :hmm: Bleh. Fixed it. Updated the wc line to the following. Code: file_lines=`wc -l ${file} | awk '{print $1}'` #2 Saint Nick, Jun 22, 2011 Last edited: Jun 22, 2011 (You must log in or sign up to post here.) Show Ignored Content Loading... Similar Threads - UNIX Shell Scripting UNIX File Abstraction and Hardware Level I/O chrstrbrts, Jan 12, 2016, in forum: Programming Replies: 8 Views: 980 exdeath Jan 16, 2016 unix shell scripting paul60, Nov 17, 2013, in forum: Programming Replies: 5 Views: 936 Childs Jan 14, 2014 unix shell scripting OogyWaWa, Jun 16, 2010, in forum: Programming Replies: 6 Views: 858 Ken g6 Jun 17, 2010 UNIX
and Signals and Traps (Oh My!) - Part 1 by William Shotts, Jr. In this lesson, we're going to look at handling errors during the execution of your scripts. The difference between a good program and a poor one is often measured in terms of the program's robustness. That is, the program's ability to handle situations in which something goes wrong. Exit status As you recall from previous lessons, every well-written program returns an exit status when it finishes. If a program finishes successfully, the exit status will be zero. If the exit status is anything other than zero, then the program failed in some way. It is very important to check the exit status of programs you call in your scripts. It is also important that your scripts return a meaningful exit status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of code: # Example of a really bad idea cd $some_directory rm * Why is this such a bad way of doing it? It's not, if nothing goes wrong. The two lines change the working directory to the name contained in $some_directory and delete the files in that directory. That's the intended behavior. But what happens if the directory named in $some_directory doesn't exist? In that case, the cd command will fail and the script executes the rm command on the current working directory. Not the intended behavior! By the way, my hapless system administrator's script suffered this very failure and it destroyed a large portion of an important production system. Don't let this happen to you! The problem with the script was that it did not check the exit status of the cd command before proceeding with the rm command. Checking the exit status There are several ways you can get and respond to the exit status of a program. First, you can examine the contents of the $? environment variable. $? will contain the exit status of the last command executed. You can see this work with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false commands are programs that do nothing except return an exit status of zero and one, respectively. Using them, we can see how the $? environment variable contains the exit status of the previous program. So to check the exit status, we could write the script this way: # Check the exit status cd $some_directory if [ "$?" = "0" ]; then rm * else echo "Cannot change directory!" 1>&2 exit 1 fi In this version, we examine the exit status of t