I have a script that lists all files in a directory, lists them in alphabetical order, and places the number of the file before the filename.
#!/bin/bash
x=1
cd ~/bin
for f in *
do
if [ -f $f ]; then
echo "$x: $f"
declare a$x=$f
x=$(expr $x + 1)
fi
done
read -p "What would you like to execute?: " num
$num
Output would be
1: file0
2: file1
3: file2
etc
Running $num will execute the command
a1
which is not a command. What I want to do is run what $a1 is equal to (ie file0). How can I do this?
It looks like you are implementing the select
built-in command:
PS3="What would you like to execute?: "
select cmd in *; do
$cmd
break
done
You must use the eval command:
eval \$a0
Hope this helps =)
EDIT: Fixed missing backslash
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.