Write a script that accepts [on hold]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-3
down vote

favorite












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.










share|improve this question







New contributor




user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as too broad by Filipe Brandenburger, JdeBP, Thomas, muru, RalfFriedl Nov 18 at 11:42


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 5




    This is a homework assignment, not a question. What part of this assignment do you have an issue with?
    – Kusalananda
    Nov 18 at 9: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 at 10:02














up vote
-3
down vote

favorite












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.










share|improve this question







New contributor




user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as too broad by Filipe Brandenburger, JdeBP, Thomas, muru, RalfFriedl Nov 18 at 11:42


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 5




    This is a homework assignment, not a question. What part of this assignment do you have an issue with?
    – Kusalananda
    Nov 18 at 9: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 at 10:02












up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











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.










share|improve this question







New contributor




user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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.







scripting






share|improve this question







New contributor




user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 18 at 9:14









user321562

1




1




New contributor




user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user321562 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as too broad by Filipe Brandenburger, JdeBP, Thomas, muru, RalfFriedl Nov 18 at 11:42


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






put on hold as too broad by Filipe Brandenburger, JdeBP, Thomas, muru, RalfFriedl Nov 18 at 11:42


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 5




    This is a homework assignment, not a question. What part of this assignment do you have an issue with?
    – Kusalananda
    Nov 18 at 9: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 at 10:02












  • 5




    This is a homework assignment, not a question. What part of this assignment do you have an issue with?
    – Kusalananda
    Nov 18 at 9: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 at 10:02







5




5




This is a homework assignment, not a question. What part of this assignment do you have an issue with?
– Kusalananda
Nov 18 at 9:25




This is a homework assignment, not a question. What part of this assignment do you have an issue with?
– Kusalananda
Nov 18 at 9:25




3




3




This is an order, not a question. Unix and Linux Stack Exchange is not a script writing to order service.
– JdeBP
Nov 18 at 10:02




This is an order, not a question. Unix and Linux Stack Exchange is not a script writing to order service.
– JdeBP
Nov 18 at 10:02










2 Answers
2






active

oldest

votes

















up vote
2
down vote













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






share|improve this answer






















  • "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" (not just directories)
    – sourcejedi
    Nov 18 at 11:36






  • 1




    @sourcejedi Yup. But not the contents of directories whose names happens to match the given pattern.
    – Kusalananda
    Nov 18 at 11:37






  • 1




    Ah, right. But "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".
    – sourcejedi
    Nov 18 at 11:38






  • 1




    Beautiful one-liner! Or ugly line noise, depending on who you ask :-).
    – sourcejedi
    Nov 18 at 11:43






  • 1




    @Debian_yadav There is nothing in the text about printing the working directory. It just says that the working directory has to be restored ("returned") to what it was before running the script). See the added bits in the answer.
    – Kusalananda
    Nov 18 at 11:46

















up vote
0
down vote













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".






share|improve this answer





























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













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






    share|improve this answer






















    • "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" (not just directories)
      – sourcejedi
      Nov 18 at 11:36






    • 1




      @sourcejedi Yup. But not the contents of directories whose names happens to match the given pattern.
      – Kusalananda
      Nov 18 at 11:37






    • 1




      Ah, right. But "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".
      – sourcejedi
      Nov 18 at 11:38






    • 1




      Beautiful one-liner! Or ugly line noise, depending on who you ask :-).
      – sourcejedi
      Nov 18 at 11:43






    • 1




      @Debian_yadav There is nothing in the text about printing the working directory. It just says that the working directory has to be restored ("returned") to what it was before running the script). See the added bits in the answer.
      – Kusalananda
      Nov 18 at 11:46














    up vote
    2
    down vote













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






    share|improve this answer






















    • "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" (not just directories)
      – sourcejedi
      Nov 18 at 11:36






    • 1




      @sourcejedi Yup. But not the contents of directories whose names happens to match the given pattern.
      – Kusalananda
      Nov 18 at 11:37






    • 1




      Ah, right. But "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".
      – sourcejedi
      Nov 18 at 11:38






    • 1




      Beautiful one-liner! Or ugly line noise, depending on who you ask :-).
      – sourcejedi
      Nov 18 at 11:43






    • 1




      @Debian_yadav There is nothing in the text about printing the working directory. It just says that the working directory has to be restored ("returned") to what it was before running the script). See the added bits in the answer.
      – Kusalananda
      Nov 18 at 11:46












    up vote
    2
    down vote










    up vote
    2
    down vote









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






    share|improve this answer














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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 18 at 12:00

























    answered Nov 18 at 11:33









    Kusalananda

    116k15218352




    116k15218352











    • "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" (not just directories)
      – sourcejedi
      Nov 18 at 11:36






    • 1




      @sourcejedi Yup. But not the contents of directories whose names happens to match the given pattern.
      – Kusalananda
      Nov 18 at 11:37






    • 1




      Ah, right. But "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".
      – sourcejedi
      Nov 18 at 11:38






    • 1




      Beautiful one-liner! Or ugly line noise, depending on who you ask :-).
      – sourcejedi
      Nov 18 at 11:43






    • 1




      @Debian_yadav There is nothing in the text about printing the working directory. It just says that the working directory has to be restored ("returned") to what it was before running the script). See the added bits in the answer.
      – Kusalananda
      Nov 18 at 11:46
















    • "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" (not just directories)
      – sourcejedi
      Nov 18 at 11:36






    • 1




      @sourcejedi Yup. But not the contents of directories whose names happens to match the given pattern.
      – Kusalananda
      Nov 18 at 11:37






    • 1




      Ah, right. But "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".
      – sourcejedi
      Nov 18 at 11:38






    • 1




      Beautiful one-liner! Or ugly line noise, depending on who you ask :-).
      – sourcejedi
      Nov 18 at 11:43






    • 1




      @Debian_yadav There is nothing in the text about printing the working directory. It just says that the working directory has to be restored ("returned") to what it was before running the script). See the added bits in the answer.
      – Kusalananda
      Nov 18 at 11:46















    "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" (not just directories)
    – sourcejedi
    Nov 18 at 11:36




    "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" (not just directories)
    – sourcejedi
    Nov 18 at 11:36




    1




    1




    @sourcejedi Yup. But not the contents of directories whose names happens to match the given pattern.
    – Kusalananda
    Nov 18 at 11:37




    @sourcejedi Yup. But not the contents of directories whose names happens to match the given pattern.
    – Kusalananda
    Nov 18 at 11:37




    1




    1




    Ah, right. But "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".
    – sourcejedi
    Nov 18 at 11:38




    Ah, right. But "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".
    – sourcejedi
    Nov 18 at 11:38




    1




    1




    Beautiful one-liner! Or ugly line noise, depending on who you ask :-).
    – sourcejedi
    Nov 18 at 11:43




    Beautiful one-liner! Or ugly line noise, depending on who you ask :-).
    – sourcejedi
    Nov 18 at 11:43




    1




    1




    @Debian_yadav There is nothing in the text about printing the working directory. It just says that the working directory has to be restored ("returned") to what it was before running the script). See the added bits in the answer.
    – Kusalananda
    Nov 18 at 11:46




    @Debian_yadav There is nothing in the text about printing the working directory. It just says that the working directory has to be restored ("returned") to what it was before running the script). See the added bits in the answer.
    – Kusalananda
    Nov 18 at 11:46












    up vote
    0
    down vote













    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".






    share|improve this answer


























      up vote
      0
      down vote













      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".






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        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".






        share|improve this answer














        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".







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 at 11:37

























        answered Nov 18 at 11:04









        sourcejedi

        21.9k43396




        21.9k43396












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay