WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Piping data into a script  (Read 341 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Piping data into a script
« on: August 12, 2024, 01:01:25 PM »
4 years ago I wrote a script called FindMagic to identify
executable files using hexdump and magic numbers.

Yesterday I used it to list all of the Tinycore scripts in /usr/bin/
for a forum member:
Code: [Select]
tc@E310:~$ ls -1 /usr/bin/ | while read F; do ./FindMagic /usr/bin/$F 2>&1 | grep Script; done
Script  /usr/bin/backup
Script  /usr/bin/bbcheck.sh
Script  /usr/bin/bigHomeFiles.sh
Script  /usr/bin/calc
Script  /usr/bin/chkonboot.sh
 ----- Snip -----

It occurred to me it should be able to handle multiple files in one
shot natively instead of calling the script multiple times, like this:
Code: [Select]
tc@E310:~$ ls -d /usr/bin/* | ./FindMagic | grep Script
Script  /usr/bin/backup
Script  /usr/bin/bbcheck.sh
Script  /usr/bin/bigHomeFiles.sh
Script  /usr/bin/calc
Script  /usr/bin/chkonboot.sh
 ----- Snip -----

So now it can handle multiple files from the command line or you
can pipe the data straight into the script:
Code: [Select]
tc@E310:~$ FindMagic

 Written by Richard A. Rost Jun 11,2020
 This script attempts to identify executable files by type.
 Types are  ELF32  ELF64  Script  Win  Dos  Unknown
 If type is Script, the first 3 lines of the script are printed.

Usage:
         FindMagic Filename1 [ Filename2 Filename3 etc ]
         ls | FindMagic
         ls -d /Path/To/Files/* | FindMagic

tc@E310:~$

A copy of the script is attached so you can see how it's done.

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Piping data into a script
« Reply #1 on: August 12, 2024, 03:54:37 PM »
How is this different from the "file" command?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: Piping data into a script
« Reply #2 on: August 12, 2024, 04:02:09 PM »
Hi andyj
They both would work. The script is a lot smaller and outputs
the format I want. It's only purpose is identifying executables.

The main point of the post was making a script pipe friendly.
« Last Edit: August 12, 2024, 04:29:53 PM by Rich »

Offline andyj

  • Hero Member
  • *****
  • Posts: 1036
Re: Piping data into a script
« Reply #3 on: August 12, 2024, 04:18:31 PM »
How about:

Code: [Select]
find /usr/bin -type f -exec file {} \; | grep POSIX

Or ELF instead of POSIX for executables, which is how I find what extensions are required for libraries when building dep files.

Try this for finding unresolved libraries:
Code: [Select]
#!/bin/sh
#
for a in $(find /tmp/tcloop -type f)
        do file -b $a | grep -q '^ELF ' && TLIB=$(ldd $a | grep found) && [ -n "${TLIB:+x}" ] && echo -e "$a\n$TLIB"
done

This assumes you are using links instead of copy.
« Last Edit: August 12, 2024, 04:20:06 PM by andyj »