Problem in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in bash script
# 8  
Old 11-18-2010
Could also be written (in ksh and bash)
Code:
numOfParameters=$(($numOfParameters + 1))

# 9  
Old 11-18-2010
Quote:
Originally Posted by radoulov
Modified version:

Code:
neededParameters=2
numOfParameters=0
correctNum=0
while getopts "s:l:" opt
do
    case $opt in         # the quotes are unnecessary in this context
    s)
        serviceName=$OPTARG # no space after the equal sign
        numOfParameters=$(( $numOfParameters + 1 )) # I suppose you need arithmetic expansion here
        ;;
    l)
        fileName=$OPTARG #errorline 2 
        numOfParameters=$(( $numOfParameters + 1 )) 
        ;;
    ?)
        echo "unknown option"
        ;;
    esac
done

Thanks you, you solved my problem.
# 10  
Old 11-18-2010
Unfortunately I know nothing about bash (ksh user...) but know some posix...
As I just found a box (AIX 6.1) that seems to have bash I gave a try...
The only other syntax that seems to work is this one:
Code:
let numOfParameters="$numOfParameters + 1"

But again Im no bash specialist and dont know what the implementation I use is worth...
# 11  
Old 11-18-2010
Quote:
Originally Posted by programAngel
O.K.

But why do you think that?

Because from what I understand when there is colons after the letter then it mean that a parameter must come after it when you run the script.
I mean it mean that if you write in the shell:
script -s
then the colons after the s (in the script) will send error.
Hope I explain myself good.
Yep i just had a doubt that's why i removed my post just after ...
so i just went back to the RTFM part ...

Colon following letter means : the letter option requires an argument
An initial colon prevent getopts from printing an error message when invalid option are given
Smilie


---------- Post updated at 04:26 PM ---------- Previous update was at 04:23 PM ----------

Quote:
Originally Posted by vbe
Unfortunately I know nothing about bash (ksh user...) but know some posix...
As I just found a box (AIX 6.1) that seems to have bash I gave a try...
The only other syntax that seems to work is this one:
Code:
let numOfParameters="$numOfParameters + 1"

But again Im no bash specialist and dont know what the implementation I use is worth...
For lazy ppl like me :
Code:
let numOfParameters++

Smilie
# 12  
Old 11-18-2010
Thanks ctsgnb!
Could you explain how
Code:
let numOfParameters++

works?

I just tried also last posix way:
Code:
 numOfParameters=$((numOfParameters + 1))

Works also...
# 13  
Old 11-18-2010
Quote:
Originally Posted by vbe
Thanks ctsgnb!
Could you explain how
Code:
let numOfParameters++

works?

I just tried also last posix way:
Code:
 numOfParameters=$((numOfParameters + 1))

Works also...
Not sure about what you mean by "how it works?" but I suppose let set the var to 0 if not initialized or amibguous (not numeric or empty) and then increment it by 1 (a different step could be defined with the i+=n notation this would do i="$i + n") where n is the numeric step of course.

So all the following are equivalent the first one is just shorter to write (and maybe less parse consuming)
Code:
let numOfParameters++
let numOfParameters+=1
let numOfParameters="$numOfParameters + 1"

I tested the following on a linux machine and it works fine with the different sh ksh bash (i don't know if it behaves the same way on AIX) :

Code:
$ ps
  PID TTY          TIME CMD
  387 pts/67   00:00:00 bash
13882 pts/67   00:00:00 sh
14052 pts/67   00:00:00 ps
$ echo $$
13882
$ echo $i
1
$ let i++
$ echo $i
2
$ exec /bin/ksh
$ ps
  PID TTY          TIME CMD
  387 pts/67   00:00:00 bash
13882 pts/67   00:00:00 ksh
14225 pts/67   00:00:00 ps
$ echo $$
13882
$ echo $i

$ let i++
$ echo $i
1
$ exec /bin/bash
$ ps
  PID TTY          TIME CMD
  387 pts/67   00:00:00 bash
13882 pts/67   00:00:00 bash
14420 pts/67   00:00:00 ps
$ echo $$
13882
$ echo $i
/etc/profile.d/which-2.sh
$ i=
$ echo $i

$ let i++
$ echo $i
1
$


Last edited by ctsgnb; 11-18-2010 at 01:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Array problem in Bash Script

I am trying to write a Bash Script using a couple of arrays. I need to perform a countdown of sorts on an array done once daily, but each day would start with the numbers from the previous day. This is what I'm starting with : #!/bin/bash days=(9 8 7 6 5) for (( i = 0 ; i < ${#days} ; i++... (4 Replies)
Discussion started by: cogiz
4 Replies

2. Shell Programming and Scripting

Bash/awk script problem

Hi, I have 100 files containing different values in single column, I want to split those files in two separate files (file2 and file3) based on average value of first column of each file, for those files I am working on the following script #bin/bash for memb in $(seq 1 100) do awk... (4 Replies)
Discussion started by: dsp80
4 Replies

3. Shell Programming and Scripting

Help with bash script problem

Hi, Below is my bash script: cat run_all.sh if && ; then Number_Count_Program $1.results $2.results > $1.$2.counts else Number_Split_Program $1.results $2.results > $1.$2.split fi After I run the following command: ./run_all.sh A B ./run_all.sh: line 1: Anybody advice to edit... (5 Replies)
Discussion started by: perl_beginner
5 Replies

4. Shell Programming and Scripting

Problem with Bash Script.

Hi guys! I'm new to the forum and to the Bash coding scene. I have the following code paths=/test/a paths=/test/b keywords=\"*car*\" keywords=\"*food*\" for file in `find paths -type f -ctime -1 -name keywords -print 2>/dev/null` do #.... do stuff here for every $file found... (5 Replies)
Discussion started by: RedSpyder
5 Replies

5. Shell Programming and Scripting

Problem using grep in bash script

When it comes to programing and UNIX, I know just enough to be really really dangerous. I have written a python script to parse through a file that contains ~1 million lines. Depending on whether a certain string is matched, the line is copied into a particular file. For the sake of brevity,... (4 Replies)
Discussion started by: errcricket
4 Replies

6. Shell Programming and Scripting

help with a bash script problem

hi to everyone :) i am new to linux and bash and i am trying to build a bash script, that is quite similar to the well known cmd 'split' ... ;) it is now already "working" ... i can use it like: ./splitfix.sh -v -s 10 foo ./splitfix.sh -s 10 -v foo ./splitfix.sh -s 10 foo ./splitfix.sh -v... (5 Replies)
Discussion started by: drjodo
5 Replies

7. Shell Programming and Scripting

problem using variables in bash script

I am using variable to give the location of the file I am using but I get error. Here is the code: LogFile=/tmp/log.email echo -e "could not close the service - error number $error \n" > $LogFile well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Discussion started by: programAngel
3 Replies

8. Shell Programming and Scripting

Simple bash script problem

#!/bin/bash cd /media/disk-2 Running ./run.sh it's not changing directory.Why? (6 Replies)
Discussion started by: cola
6 Replies

9. UNIX for Dummies Questions & Answers

Bash script argument problem

I'm having problems with bash scripts. If a bash script is called with no arguments, I always get "PHIST=!" as the first argument (i.e. this is what $1 equals). Why? Where does this come from, and how can I fix it? Nothing in the bash man pages refer to this mysterious default argument. (2 Replies)
Discussion started by: sszd
2 Replies

10. Shell Programming and Scripting

bash script problem

hi I am writing a bash script that uses dialog to get user input an diplay messages to user. I have a small problem dialog --inputbox "blabla" 20 50 2> /tmp/output VAR="'cat /tmp/output'" mkdir $VAR the code below requests user for a directory path to be created. But, if the user uses... (1 Reply)
Discussion started by: fnoyan
1 Replies
Login or Register to Ask a Question