1

I'd wish the user to select a file from a numbered list on screen in a mkinitcpio busybox ash environment. The list needs to be filled/sorted from top to bottom and printed in columns, like:

1) 2017-12-17   3) 2017-12-19   5) 2017-12-21   7) 2017-12-23  9) 2017-12-25
2) 2017-12-18   4) 2017-12-20   6) 2017-12-22   8) 2017-12-24

I am printing to screen using printf line by line. That is why you would normally use an array. Because busybox ash lacks array support the hack is to use set to create a poor man's array using positional parameters ($1=2017-12-17, $2=2017-12-18, etc.).

For now I manage to retrieve the positional parameter value using:

file=$(eval echo \$$i)

Is there a better way to retrieve the n-th positional parameter in ash?


Notes

  1. The column listing above is a simplified example, the actual file names do contain time stamps too, like for example 2017-12-27-060056.
  2. Mkinitcpio busybox 1.27.2 compile config.
  • @isaac Incorrect set -- ...;i=1;file=$i;echo $file results in output of 1 (in mkinitcpio busybox 1.27.2 ash) – Pro Backup Dec 27 '17 at 15:33
  • 1
    try set -- ...;i=2;eval file\=\$$i;echo "$file" – Isaac Dec 27 '17 at 15:48
  • @isaac set -- ...;i=2;eval file\=\$$i;echo "$file" works as expected. And is an improvement: 1 line less with set -x eneabled over the file=$(eval echo \$$i) solution. – Pro Backup Dec 27 '17 at 16:17
0

You can use shift. It's better than eval.

cat busy.sh

leshift () {
  shift "$1"
  echo "$1"
}
echo param debut
echo $#
echo $@
for i in "$@" ; do
  echo "$i"
done

echo param shift
read a # number from 1 to 5
b=$(leshift "$a" "$@")
echo $b

echo param fin
echo $#
echo $@
for i in "$@" ; do
  echo "$i"
done

You call it like that

./busy.sh a b 'c h' d e
  • When I would need the stored values in sequential order, shift is a good replacement. Because of the "random" access order and, as far as I understood, shift removes positional parameters, I don't see a solution with shift. (I don't wish to re-read the files list from disk to prevent issues because of file being written in the mean time) – Pro Backup Dec 28 '17 at 13:03
  • @ProBackup I update the post to explain. Hope this help. – ctac_ Dec 28 '17 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.