ACK_stoverflow
ACK_stoverflow

Reputation: 3305

Get sourced filename in busybox ash

The ash shell in busybox doesn't seem to implement any of the standard ways to get the filename that's being sourced. For instance:

testo:

#!/usr/bin/env -S busybox ash
echo hello whorl
echo using source
source ./sourceme
echo using .
. ./sourceme

sourceme:

echo underscore $_
echo bs $BASH_SOURCE
echo zero $0
# ./testo 
hello whorl
using source
underscore ./testo
bs
zero ./testo
using .
underscore ./testo
bs
zero ./testo

I need something to put in sourceme that will get its own name/path.

Upvotes: 1

Views: 640

Answers (1)

ACK_stoverflow
ACK_stoverflow

Reputation: 3305

This excellent answer contains a very clever way to accomplish this. I've adapted their solution here - insert this line into sourceme:

echo lsof `lsof -p $$ -Fn | tail -n1 | sed 's!^[^/]*!!g'``

And you get:

lsof /absolute/path/to/sourceme

Note: since we're talking about busybox here, this is an implementation of the above using busybox's lsof:

lsof | grep '^'$$ | tail -n1 | awk '{print $3}'

Note: if anyone finds a way to do this using some builtin mechanism from busybox ash, post an answer and I'll change the accepted answer to yours.

Upvotes: 1

Related Questions