Saving * nix format

My code works, but not the way I want. Basically, what my code does is that it looks at the current directory, looks for folders, and in these folders it looks at files. If one of the file extensions is the value of the $ Example variable, then it should delete all other files with the same source file name, regardless of the extension, and rename one with the $ Example extension with the same name, just without the $ Example extension. Here is the code:

#!/bin/sh
set -o errexit
Example=dummy
for d in *; do
    if test "$(ls -A "$d" 2>/dev/null)"; then
        if [ $(ls -1 ${d}/*.$Example 2>/dev/null | wc -l) -ge 1 ]; then
            cd $(pwd)/$d;
            for f in *.$Example; do
                fileName="${f%.$Example}";
                mv "$f" "${f%.$Example}";
                #tr "\r" "\n" < "${f%.$Example}" > "${f%.$Example}"
                listToDelete=$(find -name "$fileName.*");
                for g in $listToDelete; do
                    rm $g;
                done;
            done;
            cd ..;
        fi;
    fi;
done

VIM, , Linux Windows. - , , , \r, . , , , mv, Linux, , , , .

+4
2

\r script, , , :

#!/bin/bash

ext='dummy'
tmpfile=$(mktemp)

find . -type f -name "*.$ext" \
    -execdir bash -x -c \
        'cp "$3" "$2" &&
         tee "${3%.$1}"* <"$2" >/dev/null' bash "$ext" "$tmpfile" {} \;

rm -f "$tmpfile"

cp "$3" "$2" && tee "${3%.$1}"* <"$2" >/dev/null
  • $1 .
  • $2 .
  • $3 .

, tee, (${3%.$1} filename ${3%.$1}* , ).

find -execdir, -exec , . , {} .

:

$ ls test/
f1.bar   f1.foo   f2.bar   f2.foo   f3.bar   f3.foo
f1.dummy f1.txt   f2.dummy f2.txt   f3.dummy f3.txt

script :

$ bash -x script.sh
+ ext=dummy
++ mktemp
+ tmpfile=/tmp/tmp.9v5JMAcA12
+ find . -type f -name '*.dummy' -execdir bash -x -c 'cp "$3" "$2" &&
         tee "${3%.$1}"* <"$2" >/dev/null' sh dummy /tmp/tmp.9v5JMAcA12 '{}' ';'
+ cp f1.dummy /tmp/tmp.9v5JMAcA12
+ tee f1.bar f1.dummy f1.foo f1.txt
+ cp f2.dummy /tmp/tmp.9v5JMAcA12
+ tee f2.bar f2.dummy f2.foo f2.txt
+ cp f3.dummy /tmp/tmp.9v5JMAcA12
+ tee f3.bar f3.dummy f3.foo f3.txt
+ rm -f /tmp/tmp.9v5JMAcA12

-x bash ( bash ), .


bash, sh, sh ${3%.$1}.

0

VIM, Linux, Windows.

.

3 .

ViM

, \r \n, Windows (\r\n) . Windows - , Unix dos2unix sed vim, , , . ViM, , ViM \n \r, ViM . %s/\n/somestring/ %s/somestring/\r/g.

Cygwin

Cygwin, , ViM Windows, Unix. , .

ViM

, , GNU/Linux, , ViM, Unix :e ++ff=unix

0

Source: https://habr.com/ru/post/1681214/


All Articles