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



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

 ll

?

Thank you.

Instead of dir, try ‘ls -lFgG | grep /’ instead. Use ‘man ls’ to learn how to tweak output.

Hello,

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

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:

ll | grep '^d'

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

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

My 2 cents

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.


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

Thank you.

You can not move it. I said you can ask me. I did it.

You can not pipe onti a variable.

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: How To Ask Questions The Smart Way

Hi,

You can use shell globbing.

echo */

or using saving the directories in an array.

set -- */ "$@"
echo "$@"
for dir in "$@"; do
  echo "Directory $dir
done

or

directories=(*/)
echo "${directories@]}"
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 **](glob - Greg's Wiki)or just use find](http://http://mywiki.wooledge.org/UsingFind)which recursive by default.

Using external commands from the shell.

tree -d -L 1
find . -maxdepth 1 -type d

But tree is just for viewing things or rather just for visual purposes and not for scripting purposes.


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?

  #  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…

Hi rih5342,

(I do not come here often)

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:

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:

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

Thank you.

A very helpful comment.