Sponsored Content
Top Forums Shell Programming and Scripting Help with IF statement with loop Shell Script Post 302456723 by zero3ree on Saturday 25th of September 2010 12:47:37 PM
Old 09-25-2010
Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message.

Here is an example of the output:

input: countdown.sh 8
output: 8,7,6,5,4,3,2,1

input: countdown.sh 8 2
output: error: program must be executed with one argument.

Here is what I have so far. I am stuck because I am not sure how you differentiate between arguments and values.
Any help would be appreciated.

Code:
  #!/bin/ksh
  ###############
  # Script name: countdown.sh
  # Case #1: ./countdown.sh  <integer>
  ###########
  # Assign the first command line argument to the variable NUMBER.
  NUMBER=$1
  # One argument must be provided, otherwise don't execute
  if [$NUMBER > 1]
  then
              echo “error: program must be executed with 1 argument.”
                  exit .....
  elif [ $NUMBER=1]
  while [ $NUMBER -gt 0 ]
  do
     printf .......
     if [$NUMBER -gt 1 ]
     then
       printf ", "
     fi
   NUMBER=$(($NUMBER - 1))
  done
  printf

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script automation using case statement

Hi, I'm trying to write a shell script that has a menu and then dependant on the selection, will automate some samba file transfer. The problem is when I run the code without the case statement it runs fine. but when I put the case statement in the only way I can get the code to run is to... (6 Replies)
Discussion started by: ianf
6 Replies

2. Shell Programming and Scripting

Shell script not processing if statement properly

Hi I am trying to create a shell script that will look for a contracthead file first and if the contract head file does not exist on day1 exit script. Now on day2 if contracthead exists or not run the script uploading files in order such as contract line then contract contact so the... (2 Replies)
Discussion started by: jonathan184
2 Replies

3. Shell Programming and Scripting

shell script case statement

In a case statement like below : case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not gat a $rental for you";;... (4 Replies)
Discussion started by: sriram003
4 Replies

4. Shell Programming and Scripting

If then Else statement in shell script

Hi, I am having a shell Script which i need to modify. I am new to shell scripting , needs help. The Logic which needs to be incorporated is as follows :- The script need to check the length of first Line in a data fine and of the length of the file is > 130 then validated the input... (2 Replies)
Discussion started by: mvichare
2 Replies

5. Shell Programming and Scripting

Help With Loop in Case Statement script

I am writing a bash script that asks the user for input and I need it to repeat until the user selects quit.. I dont know how to write the loop for it I searched all over but i still do not get it.. if anyone could help with this it would be greatly apprciated here is my script so far: #!... (2 Replies)
Discussion started by: Emin_Em
2 Replies

6. Shell Programming and Scripting

Help with if statement syntax in shell script

I want to make the file test condition a variable ($Prmshn in code below). My goal is to use something like the first three unsuccessful if statetments since the 'if #!/bin/ksh test_input() { Prmshn=${1} InFLNm=${2} ifReq="-$Prmshn $InFLNm" #the following three if statments fail: #if ] ;... (10 Replies)
Discussion started by: ms63707
10 Replies

7. Shell Programming and Scripting

Print statement in shell script

Hi All, when I executed the query in normal unix mode, the output is expected. if I executed the query using shell script, I am not getting the result. not sure whether I need to use dbms_output.put_line or echo. dbms_output.put_line("COUNTRY_CODE"||" "||"SUB_TYPE"||" ... (3 Replies)
Discussion started by: arun888
3 Replies

8. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

9. Windows & DOS: Issues & Discussions

Loop statement within a batch script

I have to create a couple of files within a directory structure. Filename stays the same, but at the end of each file, the number appears. Example: File needs to be created within directory c:\temp File Name will stay the same (testfile), but extension will increase, starting at 01 - example... (1 Reply)
Discussion started by: HenkTrumpie
1 Replies

10. Shell Programming and Scripting

If statement in a shell script

Please see part of a shell script below. #Move folder to HOT folder echo " moving $PRE_OCR_DIR/$BATCHNAME to $HOT_FOLDER_DIR\n" #chmod -R 777 $PRE_OCR_DIR/$BATCHNAME mv $PRE_OCR_DIR/$BATCHNAME $HOT_FOLDER_DIR I wish to write an if statement around the move that if that if $BATCHNAME... (3 Replies)
Discussion started by: worky
3 Replies
PING6(1)							   User Commands							  PING6(1)

NAME
ping6 - Packets to network hosts SYNOPSIS
ping6 [OPTION...] HOST ... DESCRIPTION
Send ICMP ECHO_REQUEST packets to network hosts. Options valid for all request types: -c, --count=NUMBER stop after sending NUMBER packets -d, --debug set the SO_DEBUG option -i, --interval=NUMBER wait NUMBER seconds between sending each packet -n, --numeric do not resolve host addresses -r, --ignore-routing send directly to a host on an attached network -w, --timeout=N stop after N seconds Options valid for --echo requests: -f, --flood flood ping (root only) -l, --preload=NUMBER send NUMBER packets as fast as possible before falling into normal mode of behavior (root only) -p, --pattern=PATTERN fill ICMP packet with given pattern (hex) -q, --quiet quiet output -s, --size=NUMBER send NUMBER data octets -?, --help give this help list --usage give a short usage message -V, --version print program version Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options. Options marked with (root only) are available only to superuser. AUTHOR
Written by Jeroen Dekkers. REPORTING BUGS
Report bugs to <bug-inetutils@gnu.org>. COPYRIGHT
Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
The full documentation for ping6 is maintained as a Texinfo manual. If the info and ping6 programs are properly installed at your site, the command info ping6 should give you access to the complete manual. GNU inetutils 1.9 December 2011 PING6(1)
All times are GMT -4. The time now is 01:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy