Sponsored Content
Top Forums Shell Programming and Scripting Using arrays in bash using strings to bash built-in true Post 302614249 by kristinu on Wednesday 28th of March 2012 11:11:18 AM
Old 03-28-2012
Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using

Code:
/home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod

I get
Code:
hasArgument[usage] = 
hasArgument[usage] = true

Somehow the array element
Quote:
hasArgument[usage]
is returning
Quote:
true
even though I have not chosen the
Quote:
--usage
option.


Code:
#!/bin/bash

#--Initialization----------------------------------------------------------------------------------

# Recognize both the -n flag and backslashed escape sequences.
echo_style="both"

ierr=0
Dflt_rytrVblv="medium"
Dflt_vblevel=1
Dflt_shiftLen=3
Dflt_bdVblevel=1

vb_usrInputFlag=0

# --Read Command Line Arguments---------------------------------------------------------------------

# IFS is a special variable that controls splitting. We split on "|" and "=", not whitespace.
# "$*" will now become a string like a|b|c

# Split on | and =.  $* will insert the | itself when IFS="|", so we
# get back what we put in, except everything's now split on = too, i.e.
# "a b" "c" --option=file
# becomes
# "a b" "c" --option file


# Return IFS to its normal whitespace.

# Loop until all arguments are used up.
# Shift tosses the value of $1 and sets $1=$2, $2=$3, ...
# so doing this repeatedly will decrease the value of $# until
# we run out of arguments.

OLDIFS="$IFS"
IFS="|="                # IFS controls splitting. Split on "|" and "=", not whitespace.
set -- $*               # Set the positional parameters to the command line arguments.
IFS="$OLDIFS"

narg="$#"
opt_browseDir_flag=`echo "$*" | tr '' '' | awk '/BRWS/ {print 1}; ! /BRWS/ {print 0}'`
echo "\$* = $*"

echo "hasArgument[usage] = ${hasArgument[usage]}"

while [ "$#" -gt 0 ]
do

  case "$1" in

  "--"[rR][yY][tT][rR][pP][tT][hH]|\
  "--"[rR][yY][tT][rR]"-"[pP][[aA][tT][hH])
    shift               # Skip ahead one to the next argument.
    value[rytrPath]="${1}"
    hasArgument[rytrPath]=true
  ;;

  "--"[cC][mM][oO][dD]|\
  "--"[cC][mM][oO][dD]"-"[iI][fF][lL])
    shift
    value[cmodInfl]="${1}"
    hasArgument[cmodIfl]=true
  ;;

  "--"[sS][rR][cC][sS]|\
  "--"[sS][rR][cC][sS]"-"[iI][fF][lL])
    shift
    value[srcsInfl]="${1}"
    hasArgument[srcsIfl]=true
  ;;

  "--"[rR][cC][vV][sS]|\
  "--"[rR][cC][vV][sS]"-"[iI][fF][lL])
    shift
    value[rcvsInfl]="${1}"
    hasArgument[rcvsIfl]=true
  ;;

  "--"[pP][hH][sS][sS]|\
  "--"[pP][hH][aA][sS][eE][sS])
    shift
    value[phases]="${1}"
    hasArgument[phases]=true
  ;;

  "--"[vV][bB][lL][vV]|\
  "--"[vV][bB]"-"[lL][eE][vV][eE][lL])
    shift
    value[vblv]="${1}"
    hasArgument[vblv]=true
  ;;

  "--"[fF][mM][tT]|\
  "--"[fF][oO][rR][mM][aA][tT])
    shift
    value[fmt]="${1}"
    hasArgument[fmt]=true
  ;;

  "--"[dD][tT][aA][uU])
    shift
    value[dtau]="${1}"
    hasArgument[dtau]=true
  ;;

  "--"[bB][rR][aA][cC][dD]|\
  "--"[bB][rR][aA][cC]"-"[dD][iI][sS][tT])
    shift
    value[bracD]="${1}"
    hasArgument[bracD]=true
  ;;

  "--"[tT][wW][pP][tT][dD]|\
  "--"[tT][wW][pP][tT]"-"[dD][iI][sS][tT])
    shift
    value[twptD]="${1}"
    hasArgument[twptD]=true
  ;;

  "--"[tT][wW][pP][tT][iI][tT][mM][xX]|\
  "--"[tT][wW][pP][tT]"-"[iI][tT][mM][aA][xX])
    shift
    value[twptItmax]="${1}"
    hasArgument[twptItmx]=1
  ;;

  "--"[rR][aA][yY][sS]|\
  "--"[rR][aA][yY][sS]"-"[oO][fF][lL])
    shift
    value[raysOutfl]="${1}"
    hasArgument[raysOfl]=1
  ;;

  "--"[tT][rR][vV][tT]|\
  "--"[tT][rR][vV][tT]"-"[oO][fF][lL])
    shift
    value[trvtOutfl]="${1}"
    hasArgument[trvtOfl]=true
  ;;

  "--"[rR][yY][tT][rR]"-"[vV][bB]|\
  "--"[rR][yY][tT][rR]"-"[vV][bB][lL][vV])
    shift
    value[rytrVblv]="${1}"
    hasArgument[rytrVb]=true
  ;;

  "--"[pP][mM][fF][lL])
    shift
    value[pmfl]="${1}"
    hasArgument[pmfl]=true
  ;;

  "--"[rR][yY][tT][rR][cC][bB][gG]|\
  "--"[rR][yY][tT][rR][cC]"-"[bB][gG])
    hasArgument[rytrBg]=true
  ;;

  "--"[bB][dD][pP][tT][hH]|\
  "--"[bB][dD][iI][rR]"-"[pP][aA][tT][hH])
    value[bdPath]="${1}"
    hasArgument[bdPath]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD]"-"[cC][oO][lL][pP]|\
  "--"[bB][dD][iI][rR]"-"[cC][oO][lL][pP][oO][sS])
    hasArgument[bdColp]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][sS][rR][tT]|\
  "--"[bB][dD][iI][rR]"-"[sS][oO][rR][tT])
    shift
    value[bdsrt]="${1}"
    hasArgument[bdSrt]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][gG][rR][pP]|\
  "--"[bB][dD][iI][rR]"-"[gG][rR][oO][uU][pP])
    shift
    value[bdGrp]="${1}"
    hasArgument[bdGrp]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][A][L][L]|\
  "--"[bB][dD][iI][rR]"-"[A][L][L])
    hasArgument[bdAll]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][rR][yY][tT][rR]|\
  "--"[bB][dD][iI][rR]"-"[rR][yY][tT][rR])
    hasArgument[bdRytr]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][dD][rR][wW]|\
  "--"[bB][dD][iI][rR]"-"[dD][rR][wW])
    hasArgument[bdDrw]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][sS][mM][pP]|\
  "--"[bB][dD][iI][rR]"-"[sS][mM][pP])
    hasArgument[bdSmp]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][lL][oO][gG]|\
  "--"[bB][dD][iI][rR]"-"[lL][oO][gG])
    hasArgument[bdLog]=true
    hasArgument[bd]=true
  ;;

  "--"[bB][dD][mM][sS][fF]|\
  "--"[bB][dD][iI][rR]"-"[mM][sS][fF])
    hasArgument[bdMsf]=true
    hasArgument[bd]=true
  ;;

  "-"[vV][bB][lL][vV]|\
  "--"[vV][bB]"-"[lL][eE][vV][eE][lL])
    shift
    value[vbLv]="${1}"
    hasArgument[vblv]=true
  ;;

  "-q"|"--quiet")
    hasArgument[quiet]=true
    hasArgument[vb]=false
  ;;

  "-u"|"--usage")
    hasArgument[usage]=true
  ;;

  "-e"|"--examples")
    hasArgument[examples]=true
  ;;

  "-h"|"--help")
    hasArgument[help]=true
  ;;

  *)
    value[bdFileLst]="${value[bdFileLst]} ${1}"
    hasArgument[bdFileLst]=true
    hasArgument[bd]=true
  ;;

  esac

  shift                 # Skip ahead to the next argument

done

# --Print Header------------------------------------------------------------------------------------

allArgs="$*"

raytracProg="./raytrac.tcsh"
eg1="$raytracProg --cmodif"
eg2="$raytracProg --cmodif"
eg3="$raytracProg --cmodif"

echo "hasArgument[usage] = ${hasArgument[usage]}"

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: Exiting while true loop when terminal is not the focus window

I am running an Ubuntu Gutsy laptop with Advanced Compiz fusion options enabled. I am using xdotool to simulate keyboard input in order to rotate through multiple desktops. I am looking for a way to kill a while true loop when the Enter key (or Control+C if it is easier) is pushed when the... (2 Replies)
Discussion started by: acclaypool
2 Replies

2. Shell Programming and Scripting

comparing two arrays or strings in bash

Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Discussion started by: new2Linux
2 Replies

3. Shell Programming and Scripting

Arrays in bash.need help

:confused: Is it possible to delete array elements dynamically.For instance,consider an array( a b c d ) ,now can i delete array (the third element 'c').So that the array becomes array(a b d).. Thanks in advance!! (1 Reply)
Discussion started by: tj23
1 Replies

4. Shell Programming and Scripting

Searching Bash Arrays

Hi, I am writing a bash shell script. I would like to execute a statement only if an array contains a specific value. For example: array=(1 3 5 7) I would like to execute the statement only if the value 3 is present in ${array}. Thanks for any help, Mike (1 Reply)
Discussion started by: msb65
1 Replies

5. Shell Programming and Scripting

arrays in bash

hi guys, i have the following script and when i run it i get blank lines on the screen.. i am trying to display the contents of array var.. #!/usr/bin/bash var=`awk 'NR>20&&NR<31' try.sum | awk '{print $4}'` echo "${var}" (1 Reply)
Discussion started by: npatwardhan
1 Replies

6. Shell Programming and Scripting

subtraction in bash arrays

hi i am using bash shell to perform some subraction. here is what i have: i have a while loop and am using i as a counter. diff= `expr ${ARRAY1} - ${ARRAY2}` for example array1 has -0.7145 and array2 has -0.7041. when i try the above command, i get expr: non-numeric argument. any... (6 Replies)
Discussion started by: npatwardhan
6 Replies

7. Shell Programming and Scripting

Yet another bash arrays question

Hi all, I have a file that contains many lines, but only a few are of my interest, so I'm cutting it with grep + awk, and the result I get is for example line 0 line 1 line 2 line 3 line n Now I want to store each line in an array "cell" so I can use it later calling to ${array},... (2 Replies)
Discussion started by: TuxSax
2 Replies

8. Shell Programming and Scripting

bash built-in

Is there any command or VARIABLE in unix to display only bash builtin commands?. Some days back I worked on that, but now I do not remember. Can anyone please reply for this?... (4 Replies)
Discussion started by: gwgreen1
4 Replies

9. Shell Programming and Scripting

How to make arrays from strings in bash?

Hey all, This is my first post, and I am relatively new to linux/unix scripts. I am writing a bash script in which I am trying to extract one line from another file and parse specific words from the line into an array. So for example, I have a file called SortScans in which the first 5 lines... (9 Replies)
Discussion started by: camocazi
9 Replies

10. Shell Programming and Scripting

bc,getopt and arrays in bash

trying to sum elements in an array using bc and getopt,i have a file with names and thier vaules if the names appears 3 times i should multiply its value with 3 then find the sum of all the elements together cat foo.txt max 2.3 henry 3 fransis 4.5 max 2.3 henry 3 max 2.3 it should... (1 Reply)
Discussion started by: elginmulizwa
1 Replies
All times are GMT -4. The time now is 05:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy