0

I want to process a group of files.

pi@raspberrypi:~/A6.1 $ ls -1

0bd57df4.code
0bd57df4.enc
3189204c.code
3189204c.enc
39f831fb.code
39f831fb.enc
68ff6d19.code
68ff6d19.enc
find.sh
test.sh

I run a script (find.sh) that searches for files by the mask, and runs another script (test.sh) and passes the name of the found file to the parameter.

In test mode, I want to display the full file name and the shortened name without an extension.

The contents of the file find.sh:

#!/bin/bash

find -name '*.enc' -printf "%f\0" | xargs -0 -n 1 ./test.sh

The contents of the file test.sh:

# !/bin/bash

NAMEFILE=$1
FULLNAME=$NAMEFILE
CUTNAME=`echo ${NAMEFILE:0:6}`
echo "FULLNAME - "$FULLNAME
echo "FILENAME - $CUTNAME"

The full name is displayed, there is no shortened name. Gives an error message. How do I process a variable?

pi@raspberrypi:~/A6.1 $ ./find.sh
./test.sh: 1: ./test.sh: Bad substitution
FULLNAME - 68ff6d19.enc
FILENAME -
./test.sh: 1: ./test.sh: Bad substitution
FULLNAME - 3189204c.enc
FILENAME -
./test.sh: 1: ./test.sh: Bad substitution
FULLNAME - 39f831fb.enc
FILENAME -
./test.sh: 1: ./test.sh: Bad substitution
FULLNAME - 0bd57df4.enc
FILENAME -

When I run the test.sh file, it works.

pi@raspberrypi:~/A6.1 $ ./test.sh 68ff6d19.code
FULLNAME - 68ff6d19.code
CUTNAME - 68ff6d19
guntbert
  • 1,637
  • 1
    It works well for me, are you sure you described the problem correctly? – Arkadiusz Drabczyk Apr 03 '18 at 21:01
  • I think the issue is that your shebang is malformed (# !/bin/bash should be #!/bin/bash or #! /bin/bash) so the ./test.sh script is being executed by xargs default shell, which is likely sh – steeldriver Apr 03 '18 at 22:34
  • Steeldriver, thank you very much, it helped me – Ruslan Kramskoy Apr 04 '18 at 18:26
  • @steeldriver Spaces are allowed after the #!. See https://unix.stackexchange.com/questions/276751/is-space-allowed-between-and-bin-bash-in-shebang But not between the # and the !. – Kusalananda Apr 19 '18 at 20:43
  • @Kusalananda Agreed - that's why I said "should be #!/bin/bash or #! /bin/bash" - the OP has a # ! (hash-space-bang) – steeldriver Apr 19 '18 at 22:06

2 Answers2

0

I have the feeling you might be able to concentrate your code by using GNU Parallel:

find -name '*.enc' | parallel echo Full:{} Cut:{.}

Or if the code is more complicated:

#!/bin/bash

doit() {
  FULLNAME="$1"
  CUTNAME="$2"
  echo "FULLNAME - $FULLNAME"
  echo "FILENAME - $CUTNAME"
}
export -f doit

find -name '*.enc' | parallel doit {} {.}
Ole Tange
  • 35,514
0

The error comes from running the script with /bin/sh which does not have the particular parameter expansion that you use. You are using /bin/sh because you have a space between the # and the ! on the first line.

Spaces are allowed after the !: Is space allowed between #! and /bin/bash in shebang?

Also related: Which shell interpreter runs a script with no shebang?


You can easily get away with a single script here:

find . -type f -name '*.enc' -exec bash -c '
    for n do
        n=${n##*/}
        printf "Full name:\t%s\n" "$n"
        printf "Cut name:\t%s\n"  "${n:0:6}"
    done' sh {} +

Running this on the filenames that you present will produce

Full name:      0bd57df4.enc
Cut name:       0bd57d
Full name:      3189204c.enc
Cut name:       318920
Full name:      39f831fb.enc
Cut name:       39f831
Full name:      68ff6d19.enc
Cut name:       68ff6d

If you just want to remove the extension:

find . -type f -name '*.enc' -exec sh -c '
    for n do
        n=${n##*/}
        printf "Full name:\t%s\n" "$n"
        printf "Cut name:\t%s\n"  "${n%.*}"
    done' sh {} +

This yields

Full name:      0bd57df4.enc
Cut name:       0bd57df4
Full name:      3189204c.enc
Cut name:       3189204c
Full name:      39f831fb.enc
Cut name:       39f831fb
Full name:      68ff6d19.enc
Cut name:       68ff6d19
Kusalananda
  • 333,661