'KSH: define a variable in for loop with the loop counter (syntax issue)
#!/bin/ksh
today=$(date "+%b %e")
homedir=/home/
for dir in jim bob joe; do
rootfiles_today_[$dir]=$(ssh hostname "ls -lrt $homedir$dir" | grep root | grep "$today")
printf "\n root owns the following files in $dir "
printf "\n\tSize\tDate\tTime\tFile Name\n\n"
printf "$rootfiles_today_[dir]" | awk '{printf "\t" $5 "\t" $6 " " $7 "\t" $8 "\t" $9 "\n"}'
done
When I try this, the output gives me
rootfiles_today_jim=: not found
as if the variable name is keeping hold of the = instead of using it to define the variable. I can't get past this part, each variation involving quotes, [], {}, set -A, eval, (ksh88 does not have define) that i can think of either doesn't work and stops the script, or gives the same above.
How can I properly define the (dynamic? iterative? I don't know the correct term) variable name or persuade it to let the = do its job?
Secondarily, please confirm my way to call the variable once defined is going to be ${rootfiles_today_[dir]} inside the loop? What about after the loop is over, if I want to add more to the script and have these 3 vars i can call by name, I will have to either call by their specific full name or echo $@ | grep word to call a specific one, or (I am fumbling this question, I know there are many ways to do almost anything) is there another way to call one or all of them iteratively if needed in a subsequent loop? This is more of a secondary ask, the main issue is getting a useful variable defined.
All of this is simpler in Bash and newer shells, my main stumbling blocks tend to be things that are different or limited in ksh.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
