到目前为止,我的脚本可以处理该ls
命令并返回目录中的文件。现在我试图让它处理像ls -l /tmp
.有什么帮助吗?
到目前为止我的脚本:
#!/bin/bash
#set -x
clear
read -p "type a command: " file
read -r -a prog <<<"$file"
IFS=: read -r -a paths <<<"$PATH"
for val in "${prog[@]}" ; do
for path in "${paths[@]}"; do
[[ -x $path/$val ]] && "$path/$val" "$@"
done
done
答案1
也许是这样的:
#!/usr/bin/env bash
clear
IFS=: read -r -a paths <<<"$PATH"
read -p "type a command: " cmd
read -d ' ' file <<<"$cmd" #the first component of command
for path in "${paths[@]}"; do
[ -x "$path/$file" ] && eval "$path/$cmd"
done
这基本上为每个可能的路径解析运行输入的命令。