Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 17 Sep 2014, 13:49
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
scripting using only shell built-ins
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [3 Posts]  
Author Message
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 853
Location: SE

PostPosted: Sat 09 Aug 2014, 19:43    Post subject:  scripting using only shell built-ins  

Hello Smile!

I've been making a few replacements for standard utilities using only shell built-ins (don't ask why Smile Rolling Eyes ). 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:
#!/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:
# 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 Smile/ MHHP

_________________
Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285

Slackos & 214X, ... and Q6xx
Nämen, vaf.... ln -s /dev/null MHHP
Back to top
View user's profile Send private message 
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 853
Location: SE

PostPosted: Thu 04 Sep 2014, 17:03    Post subject:  

The tty-replacement is solved with only shell built-ins:
Code:
#!/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.

Confused Rolling Eyes /MHHP

_________________
Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285

Slackos & 214X, ... and Q6xx
Nämen, vaf.... ln -s /dev/null MHHP
Back to top
View user's profile Send private message 
MinHundHettePerro


Joined: 05 Feb 2009
Posts: 853
Location: SE

PostPosted: Tue 09 Sep 2014, 19:30    Post subject:  

Hello Smile!

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:
# 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:
# read </proc/1/cmdline;echo $REPLY
/bin/busybox
does not read past \0 and
Code:
# while read; do echo $REPLY;done</proc/1/cmdline
outputs nothing at all.

Going for non-shell utilities:
Code:
# A=`cat -v /proc/1/cmdline`;echo ${A//^@/ }
/bin/busybox init
works with real cat but, not with busybox cat.
Code:
# A=`tr '\0' ' ' </proc/1/cmdline`;echo $A
/bin/busybox init
works with both real tr and busybox tr.

Any pointers?
Confused/MHHP

_________________
Celeron 2.8 GHz, 1 GB, i82845, many ptns, modes 12, 13
Dual Xeon 3.2 GHz, 1 GB, nvidia quadro nvs 285

Slackos & 214X, ... and Q6xx
Nämen, vaf.... ln -s /dev/null MHHP
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [3 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0500s ][ Queries: 11 (0.0030s) ][ GZIP on ]