-3

Write a script that accepts two arguments, the firs represents a prefix string, the second a path (assume that it is an absolute path). Where the script you write must do the following

If the user inputs only one argument that is the prefix string, then it must display the list of all files that have names starting with the prefix; within the working directory. If the user inputs two arguments, that are the prefix string and a path. Then the scrip displays the list of all directories that have names starting with the prefix; within the path specified by the second argument. Note that the script must return the value of the working directory to be that before running the script.

  • 5
    This is a homework assignment, not a question. What part of this assignment do you have an issue with? – Kusalananda Nov 18 '18 at 09:25
  • 3
    This is an order, not a question. Unix and Linux Stack Exchange is not a script writing to order service. – JdeBP Nov 18 '18 at 10:02

2 Answers2

2
#!/bin/sh

( cd -- "${2:-.}" && ls -d -- "$1"*${2:+/} )

That is,

  1. cd to the directory given by the second argument, or to . if no second argument is given, or if it's empty. The -- prevents cd from interpreting anything in $2 as an option.
  2. run ls -d on all names starting with the first argument. The -d prevents the listing of the contents of any directories with matching directory names. The -- prevents ls from interpreting anything in $1 as an option. If the second argument is given, the pattern will get a trailing / and will therefore only match directory names.

This is all taking place in a subshell so that the cd does not change the working directory of the rest of the script (which is what I believe that the last sentence hints at). In any case, the script would not be able to change the working directory for the calling shell, unless that shell uses source to run the script, and the subshell prevent that from happening. The working directory is therefore restored ("returned") to what it was before running the script.

Kusalananda
  • 333,661
0

This is not possible. You cannot explicitly "return" the working directory to that before running the script. Because there is no reasonable way for the script to change the callers working directory in the first place. See: How to change the working directory of invoking shell using a script? I ignore that last sentence and continue.

#!/bin/sh
PREFIX="$1"
DIR="$2"
if [ -z "$DIR" ]; then;
    ls -d -- "$1"*
else
    cd -- "$DIR" &&
    ls -d -- "$1"*/
fi

cd -- is not strictly necessary. The problem it solves is that a string that starts with a - could be interpreted as an option to the cd command.

The question says the path is absolute, therefore it will always start with /, and never start with -. cd "$PATH" would be sufficient. However, ls -- is strictly necessary.

Note that directories are a type of file. The two-argument form of the script is required to list directories only. The one-argument form is simply required to list files. As well as directories and regular files, files include the special files for block devices, character devices, sockets, and named pipes (aka "fifo").

Linux (and other implementations) extend the list of file types. Linux includes one which is "none of the above", which I have seen the stat command print as "weird file".

sourcejedi
  • 50,249