Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: need help with a script

  1. #1
    Join Date
    Feb 2010
    Location
    nh,usa
    Posts
    167

    Default need help with a script

    I'm using leap 15.2 and KDE.

    I'm trying to write a script to get a list of directories ( from the current directory, pwd).

    Code:
    for dir in *
    do
      echo "dir = $dir"
    done
    This gets me close but using * doesn't include directory info.

    Is there a simple script to do this without parsing the whole output from a command like

    Code:
     ll
    ?

    Thank you.
    Regards,

    RIH5342

  2. #2
    Join Date
    Dec 2008
    Location
    FL, USA
    Posts
    1,964

    Default Re: need help with a script

    Instead of dir, try 'ls -lFgG | grep /' instead. Use 'man ls' to learn how to tweak output.
    Reg. Linux User #211409 *** multibooting since 1992
    Primary: 42.3,TW,15.0 & 13.1 on Haswell w/ RAID
    Secondary: eComStation (OS/2)&42.3 on 965P/Radeon
    Tertiary: TW,15.0,42.3,Fedora,Debian,more on Kaby Lake,Q45,Q43,G41,G3X,965G,Cedar,Caicos,Oland,GT218&&&

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,748

    Default Re: need help with a script

    Hello,

    I guess this would have been better in Development > Programming/Scripting (I can move it there if you want).

    Code:
    for dir in *
    will expand in all file names that are in the current working directory, not just directories.

    When you want and ll listing of only the directories inside the current working directory, then what about:
    Code:
    ll | grep '^d'
    Henk van Velden

  4. #4
    Join Date
    Jul 2018
    Location
    Loma Linda, Mo
    Posts
    129

    Default Re: need help with a script

    This is even better as it shows hidden directories but not the symbolic links.

    ls -la | grep ^d | sed 1,2d

    My 2 cents
    Opensuse 15.1 with VirtualBox VM's (Windows 98, XP, 7, 8.1, 10 & OpenSUSE 15.0)

    Unix since 1974 (pdp-11 in "B" , Interdata 7/32 in "C") (AT&T, Tandy, Convergent, IBM, NCR, HP flavors)
    Linux since 1995 (mandrake, redhat, fedora, centos, now OpenSUSE)

  5. #5
    Join Date
    Feb 2010
    Location
    nh,usa
    Posts
    167

    Default Re: need help with a script

    I don't know how to move a discussion, please feel free to move it.

    Next, how do I pipe it into a variable?

    The following line doesn't work from either a konsole command line or within a script file.

    Code:
    ls -la | grep ^d | sed 1,2d >dirs
    Thank you.
    Regards,

    RIH5342

  6. #6
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,748

    Default Re: need help with a script

    Quote Originally Posted by rih5342 View Post
    I don't know how to move a discussion, please feel free to move it.
    You can not move it. I said you can ask me. I did it.

    Quote Originally Posted by rih5342 View Post
    Next, how do I pipe it into a variable?
    You can not pipe onti a variable.
    Quote Originally Posted by rih5342 View Post
    The following line doesn't work from either a konsole command line or within a script file.

    Code:
    ls -la | grep ^d | sed 1,2d >dirs
    It does work. It does what you said to do. It redirects the output into the file dirs. Look for the file disr and you will find it.

    Now please take note, people here are willing to help you, even with a script. But most people here are not willing to spend some time when youapparently did not even tried to learn shell scripting from a tutorial, book or whatever. We can not hold you by the hand for every single step.

    It is also not very productive to just ask about one step where you do not explain what your goal is. A better explanation of this you will find at: http://www.catb.org/~esr/faqs/smart-questions.html#goal
    Henk van Velden

  7. #7

    Default Re: need help with a script

    Quote Originally Posted by rih5342 View Post
    I'm using leap 15.2 and KDE.

    I'm trying to write a script to get a list of directories ( from the current directory, pwd).

    Code:
    for dir in *
    do
      echo "dir = $dir"
    done
    This gets me close but using * doesn't include directory info.

    Is there a simple script to do this without parsing the whole output from a command like

    Code:
     ll
    ?

    Thank you.
    Hi,

    You can use shell globbing.

    Code:
    echo */
    or using saving the directories in an array.

    Code:
    set -- */ "$@"
    echo "$@"
    Code:
    for dir in "$@"; do
      echo "Directory $dir
    done
    or
    Code:
    directories=(*/)
    echo "${directories[@]}"
    Code:
    for d in "${directories[@]}"; do
      echo "Directory $d"
    done
    Note that those above is not recursive, If you want to do a recursive search either enable the shell glob globstar or just use find which recursive by default.

    Using external commands from the shell.

    Code:
    tree -d -L 1
    Code:
    find . -maxdepth 1 -type d
    But tree is just for viewing things or rather just for visual purposes and not for scripting purposes.
    Last edited by jetchisel; 19-Jan-2020 at 14:21. Reason: Add external commands
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  8. #8
    Join Date
    Feb 2010
    Location
    nh,usa
    Posts
    167

    Default Re: need help with a script

    Code:
    Now please take note, people here are willing to help you, even with a  script. But most people here are not willing to spend some time when  you apparently did not even tried to learn shell scripting from a  tutorial, book or whatever. We can not hold you by the hand for every  single step.
    OUCH! I spent several hours working on a set of test cases and finally caved in with a forum question. Searching the web for answers isn't that easy because of the syntax changes between flavors of unix and linux. There's also a big gap between you and someone from the windoz world. Thank you for your help and pointing out gaps in my thinking.

    The big picture is I am trying to run a make file.

    If the following 2 lines were executed in a makefile, the result is a variable VPATH and not a file, right?

    Code:
      #  define the search pathWhat is the result of the above 2 lines?
    dirs   := $(shell cat Filepath)
    VPATH  := $(foreach dir $(dirs),  $wildcard $(dir))
    Where Filepath is a file with directories in it.

    This is the starting point for my question.

    What is an appropriate re-coding of the 2 lines for openesuse?

    Thank you.
    Regards,

    RIH5342

  9. #9
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,793
    Blog Entries
    15

    Default Re: need help with a script

    Quote Originally Posted by rih5342 View Post
    Code:
    Now please take note, people here are willing to help you, even with a  script. But most people here are not willing to spend some time when  you apparently did not even tried to learn shell scripting from a  tutorial, book or whatever. We can not hold you by the hand for every  single step.
    OUCH! I spent several hours working on a set of test cases and finally caved in with a forum question. Searching the web for answers isn't that easy because of the syntax changes between flavors of unix and linux. There's also a big gap between you and someone from the windoz world. Thank you for your help and pointing out gaps in my thinking.

    The big picture is I am trying to run a make file.

    If the following 2 lines were executed in a makefile, the result is a variable VPATH and not a file, right?

    Code:
      #  define the search pathWhat is the result of the above 2 lines?
    dirs   := $(shell cat Filepath)
    VPATH  := $(foreach dir $(dirs),  $wildcard $(dir))
    Where Filepath is a file with directories in it.

    This is the starting point for my question.

    What is an appropriate re-coding of the 2 lines for openesuse?

    Thank you.
    Hi
    What is the application your trying to compile? Just a little snippet has no context, post the whole Makefile eg https://susepaste.org or provide a link to the source....
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  10. #10

    Default Re: need help with a script

    Hi rih5342,

    (I do not come here often)

    Quote Originally Posted by rih5342 View Post
    OUCH! I spent several hours working on a set of test cases and finally caved in with a forum question. Searching the web for answers isn't that easy because of the syntax changes between flavors of unix and linux. There's also a big gap between you and someone from the windoz world. Thank you for your help and pointing out gaps in my thinking.

    The big picture is I am trying to run a make file.

    If the following 2 lines were executed in a makefile, the result is a variable VPATH and not a file, right?

    Code:
      #  define the search pathWhat is the result of the above 2 lines?
    dirs   := $(shell cat Filepath)
    VPATH  := $(foreach dir $(dirs),  $wildcard $(dir))
    Where Filepath is a file with directories in it.

    This is the starting point for my question.

    What is an appropriate re-coding of the 2 lines for openesuse?
    For openSUSE? You are talking about make (probably GNU make), which runs on Windows too. I had that in my Windows 95/98 days, together with bash, and what not (DJGPP comes to mind).

    Now, on to your problem.

    The command info make is your friend. Enter the following command in a shell:
    Code:
    info make
    Use the down-arrow key to move the cursor downward, until it is on "* Functions::". Now press Enter.
    Move the cursor down to "* Foreach function::" and press Enter.

    On that page is an example that looks pretty much like yours.

    They use "files" instead of "VPATH" and have an extra "/*".

    Notice the difference? (hint: parentheses)

    Here is a Makefile-snippet that works:
    Code:
    dirs  := $(shell cat Filepath)
    VPATH := $(foreach dir,$(dirs),$(wildcard $(dir)))
    
    .PHONY: prvars
    prvars:
        @echo "$(dirs)"
        @echo "$(VPATH)"
    (Note: To the left of each '@', there is precisely 1 tab-character!)

    Put that snippet in a file called "Makefile", and run the command "make prvars". (Of course the file "Filepath" should exist and have some content).

    .PHONY is explaned in the info file at: Rules > Special targets.
    @ is explaned in the info file at: Recipes > Echoing.

    I hope this is enough for you to put you back on track.

    Kind regards,

    Leen

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •