- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: sh-posix: passing of arrays to functions
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 12:40 AM
тАО09-16-2004 12:40 AM
this isn't a real problem, just some syntactic sugar.
So that DBAs can read my scripts I'm refraining from Perl, and discover painfully many shell shortcomings.
Is it possible to pass arrays to functions, whose elements exceed the 9 psitional parameters without the need of shifting
(or is there such thing as passing of refs)?
I want to pass a number of files to a function that I derive from shell filename expansion (aka globbing), like such
# set -A files /app/bms/bin40/l[01][0-9][!WZ].dbg
# echo ${#files[*]}
56
Of course this won't yield what I expect
func_to_munch_files ${files[*]}
since shell will expand the 56 elements prior to passing.
Possibly yet another pseudo problem of a Perl-pampered occassional shell scriptor?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 01:46 AM
тАО09-16-2004 01:46 AM
Re: sh-posix: passing of arrays to functions
#!/usr/bin/sh
set -x
fun()
{
set -A ARR $*
# Index
i=0
while [[ $i -lt ${#ARR[*]} ]]
do
echo ${ARR[$i]}
# Or some operation
let i=i+1
done
# Return
return 0
}
# Main function here
ARRAY="test ok bye for now 1 2"
fun ${ARRAY[*]}
It will work there easily.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 01:47 AM
тАО09-16-2004 01:47 AM
Re: sh-posix: passing of arrays to functions
See the difference on ksh man page ( search $@ )
#!/usr/bin/sh
set -x
list()
{
set -A arr $@
i=0
while [[ $i -lt ${#arr[*]} ]]
do
echo ${arr[$i]}
let i=i+1
done
return 0
}
# Enter directory
echo " Directory -->"
read dir
if [[ -d $dir ]]
then
echo "Directory not found"
exit 1
fi
set -A AR `ls $dir`
list ${AR[*]}
# Exit
exit 0
Above script will list the files on the input give directory there.
Try and let know the problem if you have
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 01:53 AM
тАО09-16-2004 01:53 AM
Re: sh-posix: passing of arrays to functions
what are you are trying here,
# set -A files /app/bms/bin40/l[01][0-9][!WZ].dbg
Are you assiging files with app/bms/bin40/l[01][0-9][!WZ].dbg format there???
there try as,
# set -A files $(ls /app/bms/bin40/l[01][0-9][!WZ].dbg)
# echo ${#files[*]}
56
And sheel operation says as,
# set -A files /app/bms/bin40/l[01][0-9][!WZ].dbg
# echo ${files[*]}
/app/bms/bin40/l[01][0-9][!WZ].dbg
# echo ${#files[*]}
1
So change the one more
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 02:01 AM
тАО09-16-2004 02:01 AM
SolutionThere's no issue with supplying more than 9 parameters to a function although there will be a limit based on memory size.
As to passing of refs, you could try using eval...
Example, you have an array called FILES and a function called munch_files. Your function call will be of the form:
munch_files FILES
The function could do something like this although I'm not able to test it...
function munch_files {
eval let NUMFILES=${#${1}[*]}
let I=0
while (( I < NUMFILES));
do
eval whatever you like with ${${1}[I]}
let I=I+1
done
}
It's a bit complicated though, not easy to tell what's going on. I reckon your average DBA would struggle with it ;-)
Regards,
John
- Tags:
- eval
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 02:16 AM
тАО09-16-2004 02:16 AM
Re: sh-posix: passing of arrays to functions
your command substitution with the additional ls isn't necessary.
The shell globbing works since the files exists.
However shell funcs don't seem to be designed for long lists of parameters.
I exercised the below on the command line where I have HP-UX's /usr/bin/sh as login shell (I think it's better than the Korn shell, but not as nice as Bash ;-)
# list() { set -A ary $*;i=0;while [[ $i < ${#ary[*]} ]];do echo ${ary[$i]};((i+=1));done; }
# typeset -f list
function list
{
set -A ary $*;i=0;while [[ $i < ${#ary[*]} ]];do echo ${ary[$i]};((i+=1));done; }
# set -A files /app/bms/bin40/l[01][0-9][!WZ].dbg
# echo ${#files[*]}
56
# echo ${files[*]}|tr '\040' '\012'|tail -10
/app/bms/bin40/l11O.dbg
/app/bms/bin40/l11T.dbg
/app/bms/bin40/l12A.dbg
/app/bms/bin40/l12B.dbg
/app/bms/bin40/l12O.dbg
/app/bms/bin40/l12T.dbg
/app/bms/bin40/l13A.dbg
/app/bms/bin40/l13B.dbg
/app/bms/bin40/l13O.dbg
/app/bms/bin40/l13T.dbg
# list ${files[*]}
/app/bms/bin40/l00A.dbg
/app/bms/bin40/l00B.dbg
/app/bms/bin40/l00O.dbg
/app/bms/bin40/l00T.dbg
/app/bms/bin40/l01A.dbg
/app/bms/bin40/l01B.dbg
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 02:19 AM
тАО09-16-2004 02:19 AM
Re: sh-posix: passing of arrays to functions
Stick the elements in a (temp)file one line per name and pass the (temp)file?
Makes for easy debug / checking.
Just don't delete the (temp)file and you can see what you asked the script to work on.
Like you say, it's easy and clean in Perl! :-).
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2004 02:36 AM
тАО09-16-2004 02:36 AM
Re: sh-posix: passing of arrays to functions
I'm sure your eval-ing will work.
But as you said, this is getting a bit too obfuscated (then I could have sticked to Perl ;-)
Hein,
I wouldn't want to park data in a temporary file (although the mktemp command would be reasonably safe), why would I try to use arrays then?
But if the No. of files was approching 1024 the globbing wouldn't be such a good idea anymore.
Also as John said the shell is soon reaching its limitation as far as word splitting is concerned (or characters with long path names).
I think I have to get used to global scoping in the shell (something you really feel uneasy about once you got accustomed to lexikal scoping and the strict pragma in Perl), and avoid any passing of longer lists of arguments to shell functions.