Using arrays in bash using strings to bash built-in true


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using arrays in bash using strings to bash built-in true
# 1  
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]}"

# 2  
Old 03-28-2012
BASH doesn't have boolean types. The existence of a program named 'true' doesn't change this.

BASH doesn't have associative arrays, either. You can't use words as array indexes.
# 3  
Old 03-28-2012
Quote:
Originally Posted by Corona688
BASH doesn't have boolean types. The existence of a program named 'true' doesn't change this.

BASH doesn't have associative arrays, either. You can't use words as array indexes.
That's a bit of a problem. Doing
Quote:
man bash
in section arrays, says that Bash provides one-dimensional indexed and associative array variables. Should work from bash 4.0 onward.

ksh should have associative arrays and can use the same logic. What would be the alternative to the true/false scheme I used. For single variables, the true/false should work. Or am I wrong in the conclusion. I tried it and did not encounter a problem.

---------- Post updated at 11:18 AM ---------- Previous update was at 10:37 AM ----------

This is funny

Code:
echo "HELLO"
hasArgument[mytest0]=0
hasArgument[mytest1]=1
hasArgument[mytest2]=2
echo "${hasArgument[mytest0]} ${hasArgument[mytest1]} ${hasArgument[mytest2]}"

This returns

Code:
HELLO
2 2 2

# 4  
Old 03-28-2012
If you only need your script to run in the 1% of the world that has a new enough version of bash, yes...

Try quoting your strings.

Code:
VAR['asdf']=1
echo "${VAR['asdf']}"

# 5  
Old 03-28-2012
Quote:
Originally Posted by Corona688
If you only need your script to run in the 1% of the world that has a new enough version of bash, yes...

Try quoting your strings.

Code:
VAR['asdf']=1
echo "${VAR['asdf']}"

What's the string quoting about?
# 6  
Old 03-28-2012
It works either way for me, but I doubt we have identical bash versions, try it, see if it works.
# 7  
Old 03-28-2012
Quote:
Originally Posted by Corona688
If you only need your script to run in the 1% of the world that has a new enough version of bash, yes...

Try quoting your strings.

Code:
VAR['asdf']=1
echo "${VAR['asdf']}"

How about ksh? Can I use associative arrays without worry about using them?

Can one do the following in bash?
Code:
false=0
true=1

It works but one has to define the array as associative using:
Code:
declare -A VAR


Last edited by kristinu; 03-28-2012 at 01:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question