Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. bash Command line substitution in for loop
  2. `#!/bin/bash
  3.  for((i=1;i<"$#";i++)) do
  4.         printf "Position %s of argumentArray has  %s n", $i  $(($i))
  5.   done`
  6.        
  7. for((i=1;i<"$#";i++)) do
  8. printf "Position %s of argumentArray has  %s n", $i  $"$( eval echo $i )"
  9. done
  10.        
  11. ./test.sh  first second third
  12.  
  13. Position 1 of argumentArray has  1
  14. Position 1 of argumentArray has  2
  15. Position 1 of argumentArray has  3
  16.        
  17. Position 1 of argumentArray has  first
  18. Position 1 of argumentArray has  second
  19. Position 1 of argumentArray has  third
  20.        
  21. for((i=1;i<="$#";i++))
  22.   do
  23.       case "$($i)" in
  24.        .......
  25.        
  26. arguments=($@)
  27. for ((i = 0; i < ${#arguments[@]}; i++)); do
  28.     echo "arguments[$i] = ${arguments[$i]}"
  29. done
  30.        
  31. $ ./args.sh first second third
  32. arguments[0] = first
  33. arguments[1] = second
  34. arguments[2] = third
  35.        
  36. for((i=1;i<=$#;i++)) do
  37.     printf "Position %s of argumentArray has  %s n" $i  "${!i}"
  38. done