WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11631
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: 11631
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 »

Offline polikuo

  • Hero Member
  • *****
  • Posts: 725
Re: Piping data into a script
« Reply #4 on: November 15, 2024, 05:06:59 AM »
Hi

Just sharing my old notes on this topic.
Found these solutions on stack overflow years ago.
It's not as fancy as Rich's script, but it shows a slightly different route.

Code: [Select]
busybox find . -type f -exec head -c 4 {} \; -exec echo " {}" \; | awk '/^.ELF/{print $2}'
busybox find . -type f -exec head -c 4 {} \; -exec echo {} \; | awk '/^.ELF/{print substr($0,5)}'
busybox find . -type f -exec busybox head -c4 {} ';' -print | busybox awk '/^\x7fELF/{print(substr($0,5))}'
busybox find . -type f -exec busybox hexdump -n 4 -e '4/1 "%X"' {} ';' -exec echo {} ';' | busybox awk '/^7F454C46/{print substr($0,9)}'
findELF() {
  # files in ELF format
  busybox find "$1" -type f \
  -exec busybox head -c4 {} ';' \
  -print | busybox awk '/^\x7fELF/{print(substr($0,5))}'
}
findShebang() {
  # files starts with '#!'
  busybox find "$1" -type f \
  -exec busybox head -c2 {} ';' \
  -print | busybox awk '/^\x23\x21$/{print substr($0,3)}'
}
« Last Edit: November 15, 2024, 05:13:30 AM by polikuo »