1

My script so far can handle the ls command and returns the files in the directory. Now i am trying to make it handle commands like ls -l /tmp . Any help?

My script so far:

#!/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
2
  • I thing tha the problem is at $@ .Is there any way to take the rest part (-l /tmp) of the` ls -l /tmp`
    – Spyros_av
    Nov 12, 2015 at 21:50
  • You want to split the user's input into an array? Because you could simply get the whole line and run it.
    – Kira
    Nov 12, 2015 at 21:53

1 Answer 1

0

Maybe like this:

#!/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

This basically runs the inputted command for every possible PATH resolution.

2
  • Your script also handles corect commands with pipes . Am i right? @PSkocik
    – Spyros_av
    Nov 14, 2015 at 16:57
  • That is correct. Nov 14, 2015 at 17:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .