Need Help with function that has multiple ranges


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help with function that has multiple ranges
# 1  
Old 05-11-2011
Need Help with function that has multiple ranges

I have a problem that I cant figure out what to do in this function i need it to count multiple ranges like 0 10, 5 10, 1 100. I know that my counter needs to be outside the function and that it needs to be set to the lowest numbered positional parameter but thats were I am having trouble if anybody can take look at the code and help me out I would appreciate the help.

Here is the code:
Code:
#! /bin/bash 

count_up () 
{
  echo "Couting UP"
  n=0 
  until [[ $n -gt $1 ]]
  do
    printf "$n \n" 
    n=$(( $n + 1 )) 
  done 
}

count_down () 
{
  echo "Couting DOWN"
  n=$1
  while [[ $n -gt 0 ]]
  do
    printf "$n \n" 
    n=$(( $n - 1 )) 
  done 
} 

count_up $1
count_down $1


Last edited by Scott; 05-11-2011 at 06:16 PM.. Reason: Please use code tags and indent code
# 2  
Old 05-11-2011
Code:
man seq

Code:
# seq 3 10
3
4
5
6
7
8
9
10

What is your final goal behind this ? what are you trying to achieve?

Just a quick shot :
Code:
# cat tst
#!/usr/bin/bash
start=0
echo "start=$start"

count(){
case $1 in
    -u) let start+=$2;echo "+$2";;
    -d) let start-=$2;echo "-$2";;
    *) echo "Error : first arg should be [-u|-d]
esac
echo "start=$start"
}

count -u 5
count -d 10
count -u 3
count -u 7
count -u 40
count -d 17

Code:
# bash tst
start=0
+5
start=5
-10
start=-5
+3
start=-2
+7
start=5
+40
start=45
-17
start=28


Last edited by ctsgnb; 05-11-2011 at 01:22 PM..
# 3  
Old 05-11-2011
function

My goal is when i run the script to specify the numbers to count between low and high.
# 4  
Old 05-11-2011
Isn't it what the seq command does ? (just as demonstrated above)

Try to enter these commands :
Code:
seq 0 10
seq 5 10
seq 1 100

Or could you please provide a full example of what input and output you expect (just as i did) so it may help us to understand your needs and provide a more accurante answer to you.
Thanks
# 5  
Old 05-11-2011
Also when i run the script which i called lab5-4sh and when I want to run the script like this bash lab5-4.sh 1 10 i want it to have this output
$ ./lab5-4sh 1 10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

that is what my goal is

can you help me with that I am a beginner to shell scripting so its hard for me to understand how to write the script
# 6  
Old 05-11-2011
Code:
# cat ./lab5-4sh
#!/usr/bin/ksh
[[ $# -ne 2 ]] && echo "error 2 args required" && exit 1
[[ $1 -gt $2 ]] && ( min=$2 ; max=$1 ) || ( min=$1 ; max=$2 )
seq $min $max | xargs
seq $min $max | tail -r | xargs

You may have to enforce the code to check that the $1 and $2 entries are numbers.
as well as find another way to handle the formatting not to depend on MAX_ARGS limitation due to the xargs usage , but it can still be a beginning.
# 7  
Old 05-11-2011
When i put this in my script:
Code:
seq 0 10
seq 5 10
seq 1 100

" #! /bin/bash 

count_up () 
{
  echo "Couting UP"
  n=0 
  until [[ $n -gt $1 ]]
  do
    printf "$n \n" 
    n=$(( $n + 1 )) 
  done 
}


count_down () 
{
  echo "Couting DOWN"
  n=$1
  while [[ $n -gt 0 ]]
  do
    printf "$n \n" 
    n=$(( $n - 1 )) 
  done 
} 

count_up $1
count_down $

seq 1 10 
seq 5 10 
seq 1 100 "

the output that I get when I run the script "bash lab5-4.sh 1 10" i get this:
Code:
bash lab5-4.sh 1 10
Couting UP
0 
1 
Couting DOWN
lab5-4.sh: line 19: [[: $: syntax error: operand expected (error token is "$")
1
2
3
4
5
6
7
8
9
10
5
6
7
8
9
10
1
2
3
4
5
...
...
97
98
99
100

and that is not the output that I am trying to get it needs to look like this :

Last edited by Scott; 05-11-2011 at 06:18 PM.. Reason: Please use code tags and indent code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to call Oracle function with multiple arguments from shell script?

Dear All, I want to know how can i call oracle function from shell script code . My oracle function have around 5 input parameters and one return value. for name in *.csv; do echo "connecting to DB and start processing '$name' file at " echo "csv file name=$x" sqlplus -s scoot/tiger <!... (2 Replies)
Discussion started by: Balraj
2 Replies

2. Shell Programming and Scripting

[bash] wanted: function with a clean way for multiple return values

Hi, I have a small part of a project which is done as a bash script. bash was selected as an portability issue that works out of the box. In this script I have an exec shell-function, a wrapper around arbitrary commands. I want to have STDOUT, as an addon STDERR and the EXIT-CODE of a specified... (5 Replies)
Discussion started by: stomp
5 Replies

3. Shell Programming and Scripting

Sum values of specific column in multiple files, considering ranges defined in another file

I have a file (let say file B) like this: File B: A1 3 5 A1 7 9 A2 2 5 A3 1 3 The first column defines a filename and the other two define a range in that specific file. In the same directory, I have also three more files (File A1, A2 and A3). Here is 10 sample lines... (3 Replies)
Discussion started by: Bastami
3 Replies

4. Shell Programming and Scripting

Using multiple gsub() function under a loop in awk

Hi ALL, I want to replace string occurrence in my file "Config" using a external file named "Mapping" using awk. $cat Config ! Configuration file for RAVI ! Configuration file for RACHANA ! Configuration file for BALLU $cat Mapping ravi:ram rachana:shyam ballu:hameed The... (5 Replies)
Discussion started by: useless79
5 Replies

5. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

6. Shell Programming and Scripting

How to convert multiple number ranges into sequence?

Looking for a simple way to convert ranges to a numerical sequence that would assign the original value of the range to the individual numbers that are on the range. Thank you given data 13196-13199 0 13200 4 13201 10 13202-13207 3 13208-13210 7 desired... (3 Replies)
Discussion started by: jcue25
3 Replies

7. Shell Programming and Scripting

extracting columns falling within specific ranges for multiple files

Hi, I need to create weekly files from daily records stored in individual monthly filenames from 1999-2010. my sample file structure is like the ones below: daily record stored per month: 199901.xyz, 199902.xyz, 199903.xyz, 199904.xyz ...199912.xyz records inside 199901.xyz (original data... (4 Replies)
Discussion started by: ida1215
4 Replies

8. Programming

Passing multiple values from a function in C

I know multiple values can be returned from a function in C like this: char **read_file ( char * , unsigned long int * );//this is the function prototypeunsigned long int number_of_words = 0;//variable defined in main() and initialized to 0words_from_dictionary = read_file ( "dictionary.dit" ,... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

9. Shell Programming and Scripting

How to achieve Parellelism for the function which has multiple cp commands

Hi, Here is my requirement. Currently, there is a function which gets called in a for loop 2 times. doCopy() { cp /src/a*.jar /target/ cp /src/b*.jar /target/ cp /src/c*.jar /target } Since it is called sequentially from the for loop, I was asked to make parellel copy to... (7 Replies)
Discussion started by: kgsrinivas
7 Replies

10. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies
Login or Register to Ask a Question