2

I have a directory structure like this:

├── lebenslauf
│   ├── lebenslauf.out
│   └── lebenslauf.tex
├── thirdPage
│   ├── auto
│   │   ├── test.el
│   │   └── thirdPage.el
│   ├── missfont.log
│   ├── thirdPage.aux
│   ├── thirdPage.log
│   ├── thirdPage.pdf
│   └── thirdPage.tex
└── titlePage
    ├── titlePage.aux
    ├── titlePage.fdb_latexmk
    ├── titlePage.fls
    ├── titlePage.log
    ├── titlePage.pdf
    ├── titlePage.tex
    └── titlePage.tex~

I want to perform some actions on titlePage.tex and lebenslauf.tex. For that purpose, I want to print out the relative path of its location:

desired result:

$ ./script
lebenslauf/lebenslauf.tex
titlePage/titlePage.tex

That means, I want to have the path thirdPage/ completely ignored. Therefore, I think this question isn't helpful for my purposes. Additionally, I'm curious what causes the problem in my script, so I don't want to have a solution using e.g. find.

I have written the following script for that purpose:

#!/usr/bin/env bash
IFS=$'\n\t'
set -euo pipefail

d=( lebenslauf titlePage )
for file in "./${d[@]}"
do
    if [[  $file =~ *.tex  ]]
    then
        echo $file
    fi
done

Which prints out just nothing. I fiddled with some variants of that, but nothing works. This is in my opinion the "best" result of what I achieved so far.

uuu
  • 722
  • If your input is just those two filenames you could replace it all with echo */titlePage.tex */lebenslauf.tex ? – Jeff Schaller May 12 '16 at 22:19
  • @JeffSchaller Well, i used "echo" just for the purpose to remove unneccessary complexity of the script. So no, i could not use your suggestion, as there is a lot more involved (= about 50 lines of code) than that simple operation. – uuu May 13 '16 at 19:41

3 Answers3

2

*.tex is a pattern, not a regular expression. To match against a pattern, don't use the =~ operator.

Also, your loop iterates over the directories, not the files inside them.

d=( lebenslauf titlePage )
for dir in "${d[@]}" ; do
    for file in "$dir"/*.tex ; do
        echo $file
    done
done
choroba
  • 47,233
2

Unless you need to act on the each directory as a whole, you can make a single loop to enumerate all the files. Activate the ksh-style extended patterns to get or-patterns.

shopt -s extglob
for file in @(lebenslauf|titlePage)/*.tex; do
  echo "$file"
done

N.B. Always use double quotes around variable substitutions.

-1

much simpler would be probably:

ls */file1.tex */file2.tex

or

ls */*.tex | egrep '/(file1|file2).tex'

or

ls */*.tex | grep -v 'thirdPage/'

depending on how much you wish to type...

gena2x
  • 2,397
  • Well, for my purposes this is actually more complicated, because I want to execute approximately 50 sed commands on it and run xelatex on it afterwards. – uuu May 12 '16 at 16:21
  • ls [lt]/.tex | xargs sed -i 's/1/2/g' – gena2x May 12 '16 at 16:51
  • 1
    With some knowledge of shell, it's often easy to do very powerful things typing just few keys. in cases there others require sophisticated scripts. I would say that you only need to write a script if you really want to do something repeatedly later. Otherwise shell history is much simpler and faster. – gena2x May 12 '16 at 16:56
  • this was a part of a script :) – uuu May 12 '16 at 17:55