Is there ANY way at all to tell whether or not a variable is an array in unix?
Is there ANY way at all to tell whether or not a variable is an array in unix?
Do you mean tell if a shell variable is an array? Unices don't have a concept of "arrays", but shells do.
In bash, "$#" tells you the length of a variable, so "${#foo[*]}" (or "${#foo[@]}") will tell you the number of items in $foo. If "[ ${#foo[*]} -gt 1 ]", then $foo is an array. In csh, tsch and zsh, use "$#foo".
Note: in bash, "$#foo" is the same as "${#foo[0]}", which is the same as the string length of the first item of $foo. Also, a non-array variable in bash can often (always?) be treated as an array of length 1. In zsh, a variable holding a string can be treated as an array of characters ("${#foo}" is the number of characters, "${foo[$i]}" is the $i-th character).
ok, but is there any way to send an array as a single argument?
for example, if someone ran a script where $var is an array:
is there some way it could be accessed, for example, like this?Code:$ somescript $var
Code:#!/bin/bash echo ${1[*]}
What is your end goal in all of this? When posting a question, make sure you describe the goal, not just the step. There's a decent chance you don't need arrays to do what you want to do. It looks like my guess that you were talking about shell programming was right, but I shouldn't have to guess. Always describe the environment you're dealing with (machine, OS, application, whatever). This is part of being precise and informative about your problem.
The man page for bash will answer all of your questions and more. `man bash`:
Thus if $var holds an array, `somescript $var` is equivalent to `somescript ${var[0]}`Referencing an array variable without a subscript is equivalent to referencing element zero.
That is, `somescript "${var[*]}"` will pass $var as a single argument to somescript while `somescript "${var[@]}"` will pass each element of $var as a separate argument.Originally Posted by `man bash`
What happens if you try that? You'll learn more by playing around (& reading documentation) than by asking how someone else would do something.
Within a script, try:Originally Posted by `man bash`
and:Code:#!/bin/bash for i in $@; do echo $i done
The following is also of interest:Code:#!/bin/bash for i in "$@"; do echo $i done
This suggests trying:Originally Posted by `man bash`
from a command prompt. Now play around with $bar.Code:foo="abc def ghi jkl" bar=($foo)
Also try the script:
Pass different things on the command line to the various scripts:Code:#!/bin/bash foo=($@) echo ${#foo[*]}
- somescript abc def ghi
- somescript "abc def ghi"
- somescript "ab c" "def" "g h i"
- foo="abc def ghi jkl" somescript $foo
Again, you might not need arrays to accomplish your goal.
Just curious, what does the @ symbol represent/how is it used?
in what context? $@ represents all of the arguments passed to a script.
Edit:
this is the script i'm writing, and i know it seems trivial, but its what i'm doing:
i keep getting an error with exit status 162 (which i defined as 'too many args passed'.) so it is sending the array as seperate values even when i write it as:Code:#!/bin/bash # # arraystr # By: James Brumond # Created: 6 February, 2009 # # Compiles an array into a single string. # Use: arraystr [ -d del ] ARRAY STRING # # If the -d option is passed, then the value `del` # refers to the delimiter between the array indecies. # If the -d option is not passed, indecies are not # delimited at all. # # Exit Code Definitions: # 161 - Too Few Args Passed. # 162 - Too Many Args Passed. # helpmsg () { echo 'USE: arraystr [-d del] ARRAY STRING OPTIONS: -h, --help Displays this help message. -d del Sets the delimiter to del. +d Does not delimit indecies. (default)' exit $1 } startup () { if [ $# -eq 0 -o $1 = -h -o $1 = --help ] then helpmsg 0 elif [ $# -eq 1 ] then echo -e "Invalid Use!\n" helpmsg 161 elif [ $# -eq 2 ] then if [ $1 = -d -o $1 = +d ] then echo -e "Invalid Use!\n" helpmsg 161 else x=0 count=${#1[*]} while [ $x -le $count ] do typeset $2[$x]=`echo $2`${1[$x]} x=$((++x)) done exit 0 fi elif [ $# -eq 3 ] then if [ $1 = +d ] then x=0 count=${#2[*]} while [ $x -le $count ] do typeset $3[$x]=`echo $3`${2[$x]} x=$((++x)) done exit 0 else echo -e "Invalid Use!" helpmsg 161 fi elif [ $# -eq 4 ] then if [ $1 = -d ] then : # this part under construction... else echo -e "Invalid Use!" helpmsg 161 fi else echo -e "Invalid Use!" helpmsg 162 fi } startup $@
Code:arraystr ${var[*]} other
Last edited by kbjradmin; 02-07-2009 at 08:20 PM. Reason: Automerged Doublepost
Your script is unnecessary.
because "${name[*]}" expands to a single word with the value of each array member separated by the first character of the IFS special variable (which is a space character by default).
See why it's good to post your overall goal along with the specific step?
If you haven't yet followed one of the links and read "How To Ask Questions The Smart Way", I recommend you do it now. If you apply the advice in that document, you'll get answers faster.
Last edited by misson; 02-09-2009 at 12:57 AM.
regardless of if there is a reason for the script or not, i'm telling you that it sends the array as separate arguments, not a single one.