ARGV and ARGC in bash 3 and bash 3.2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ARGV and ARGC in bash 3 and bash 3.2
# 1  
Old 06-27-2011
ARGV and ARGC in bash 3 and bash 3.2

Hi Folks,

I've prepared a shell script that takes action based on arguments and number of arguments..sample code like:
Code:
ARGV=("$@")
ARGC=("$#")

case ${ARGV[1]} in 

abc) 
      if [ $ARGC -eq 3 ]; then
      ......
      else
           printf  "\nInvalid number of arguments, please check the inputs and try again\n"
fi;;

efg) ... and so on

Now the issue is that I wrote and run that script successfully in bash 3.2 on Max OSX. But when tried to run the same on bash 3 on Solaris it gives error and stop:
Code:
'ARGV=' unexpected

I was wondering how the script ran successfully on bash 3.2 and what piece i m missing here to get it work on bash 3?

Really appreciate your help.

Thanks and Regards,
SBC

Last edited by Franklin52; 06-28-2011 at 06:07 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 06-28-2011
Not a bash guy, just a man page reader: Man Page for bash (All Section 1) - The UNIX and Linux Forums

ARGC is not an array, why use ()?

Did you 'declare -a ARGV' to tell bash it is an array? Maybe the later bash assumes you meant that. Later stuff is like that.

Seems silly, maybe AR, Smilie, why not:
Code:
while [ $# != 0 ]
do
case "$1" in
(abc)
  ...
  ;;
(efg)
  ...
  ;;
esac
shift
done

PS: Use both () in case cases so % in vi still works.
# 3  
Old 06-29-2011
Thanks for the response DG, yes I already tried declaring the arrays explicitly but it didnt work. Would like to understand the difference what else could be the issue?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Homework & Coursework Questions

Help using argc/argv in assignment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: First, create a "hello world" program that prints "Hello World". But NOW, instead use argc to verify that a... (9 Replies)
Discussion started by: miniviking10
9 Replies

4. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

5. Shell Programming and Scripting

argc/ argv in awk

Hi guys, i'm trying to solve this problem. I have to run something like cat file1.txt | awk -f script.awk 10 if i'm in the awk script, how can i take the parameter :10 ??:wall: i try something like : BEGIN{ var=argv } {..} END{..} but obviously is not correct... (5 Replies)
Discussion started by: heaven25
5 Replies

6. Programming

Building an argc/argv style structure from a string (char*)

Hello All, First post. I've been struggling with the following: Given a char* string, I need to construct an "int argc, char *argv" style structure. What I'm struggling with most is handling escaped-whitespace and quotes. e.g. the string: char *s = "hello world 'my name is simon'... (10 Replies)
Discussion started by: cbarwise
10 Replies

7. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

9. Programming

dbx debugger + argv[argc]

Is it possible to use the dbx debugger with the CL options for the executable ? Say you have created a executable called myfunc which can take string arguments at run-time. You run it like this ./myfunc Hello World where Hello and World are the string arguments My question is whether... (1 Reply)
Discussion started by: JamesGoh
1 Replies

10. Programming

Using argv argc

I searched on the forums. No advises. I am using a previous source code. I changed the main function main(int argc, char **argv) in a function misc(int argc, char **argv). How do you use the argc and argv parameters? This is how I am calling the function : char param; strcat(param,"wgrib ");... (4 Replies)
Discussion started by: Akeson Chihiro
4 Replies
Login or Register to Ask a Question