scripting using only shell built-ins

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

scripting using only shell built-ins

#1 Post by MinHundHettePerro »

Hello :)!

I've been making a few replacements for standard utilities using only shell built-ins (don't ask why :) :roll: ). But, now I'm really stuck at this "tty"-replacement; I cannot find a way to avoid "ls" - is it at all possible?

tty

Code: Select all

#!/bin/sh

# Uses "ls"

FILE=/proc/$$/fd/0

### readlink -f
while [  -L $FILE ]
do
	A=`ls -l $FILE`
	FILE=`echo ${A#* -> }`
done
### /readlink -f

echo $FILE 

Of course, I can find out if the file is a link:

Code: Select all

# A=`echo /proc/$$/fd/0`
# [ -L $A ] && echo link
link
#
but, how can I de-reference/follow it, using only shell built-ins?

Is it at all possible, or am I banging my head up the wrong tree?

cheers :)/ MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

#2 Post by MinHundHettePerro »

The tty-replacement is solved with only shell built-ins:

Code: Select all

#!/bin/sh
# MHHP, 140811

MAJmin(){
	 INP=$7
	 # Major is contained in bits 8-15
	 # Minor (low bits of) is contained in bits 0-7
	 (( $7 >= 65536 )) && exit 1 # If >=65536, high bits of minor are contained in bits 20-31
	 (( MAJ = $INP/256 ))
	 (( MIN = $INP-$MAJ*256 ))
}

MAJmin `< /proc/$$/stat`

case $MAJ in
	#### Could check /proc/devices for major numbers ...
	136)
		DEV="/dev/pts/$MIN"
	;;
	4)
		DEV="/dev/tty$MIN"
	;;
esac

[ -c $DEV ] && echo $DEV
But, I still wonder if it is possible to de-reference a symbolic link using only shell built-ins?
[ -L link ] is obviously true for a link but, at the same time, [ -c link ] is also true for a link targetting a character special. So, some de-referencing is partly possible with only shell built-ins.

:? :roll: /MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

User avatar
MinHundHettePerro
Posts: 852
Joined: Thu 05 Feb 2009, 22:22
Location: SE

#3 Post by MinHundHettePerro »

Hello :)!

I'm at it again; this time I cannot avoid using tr.

I want to read a file with NUL-separated strings to a variable, using only shell built-ins, e.g. /proc/1/cmdline

Code: Select all

# hexdump -C /proc/1/cmdline 
00000000  2f 62 69 6e 2f 62 75 73  79 62 6f 78 00 69 6e 69  |/bin/busybox.ini|
00000010  74 00                                             |t.|
00000012

Code: Select all

# read </proc/1/cmdline;echo $REPLY 
/bin/busybox
does not read past \0 and

Code: Select all

# while read; do echo $REPLY;done</proc/1/cmdline
outputs nothing at all.

Going for non-shell utilities:

Code: Select all

# A=`cat -v /proc/1/cmdline`;echo ${A//^@/ }
/bin/busybox init
works with real cat but, not with busybox cat.

Code: Select all

# A=`tr '\0' ' ' </proc/1/cmdline`;echo $A
/bin/busybox init
works with both real tr and busybox tr.

Any pointers?
:?/MHHP
[color=green]Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285[/color]
Slackos & 214X, ... and Q6xx
[color=darkred]Nämen, vaf....[/color] [color=green]ln -s /dev/null MHHP[/color]

Post Reply