Find the answer to your Linux question:
Results 1 to 2 of 2
Here is a rather ugly piece of script that replaces HH in a string with a value that depends on an index, then executes the resulting string. Is there a ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Location
    Christchurch, NZ
    Posts
    9

    [SOLVED] Replacement according to an index

    Here is a rather ugly piece of script that replaces HH in a string with a value that depends on an index, then executes the resulting string. Is there a more elegant way to do it? In Matlab, I'd have a cell array HH={'00','06','12','18'}; which I can address using HH{$imx}. Is such a thing possible in a bash script?

    Code:
    # Depending upon imx, replace HH by the appropriate hour value
    if [ $imx = 1 ]
    then
    	${str/HH/00}
    elif [ $imx = 2 ]
    then
    	${str/HH/06}
    elif [ $imx = 3 ]
    then
    	${str/HH/12}
    elif [ $imx = 4 ]
    then
    	${str/HH/18}
    else
    	echo $imx '  is outside the range'
    fi

  2. #2
    Just Joined!
    Join Date
    May 2009
    Location
    Christchurch, NZ
    Posts
    9
    Problem solved. I missed the chapter on arrays in the text book
    Code:
    HH=(00 06 12 18)
    ${str/HH/${HH[$imx]}}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...