bashbox: multicall bash script

Under development: PCMCIA, wireless, etc.
Message
Author
2lss
Posts: 225
Joined: Sun 20 Sep 2009, 23:54

#31 Post by 2lss »

technosaurus wrote:just save this into a script to test the dir_tree portion

Code: Select all

#!/bin/sh 
DIR=$1 
[ $2 ] && SPACING=$2 || SPACING="|" 
for x in `ls $DIR`; do 
[ -d $DIR/$x ] &&  echo "$SPACING\`-{"$x && $0 $DIR/$x "$SPACING  " 
done
Sorry to get off subject, but this would work for me. Only problem is when there is a space in the filename.

'for' splits the input by line, and any file name with a space in it throws it off.

Normally I would use double quotes around a variable to prevent this, but with the for/next loop I am not sure.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#32 Post by technosaurus »

as an alternative you can simply use

Code: Select all

DIR=${1:-`pwd`}
SPACING=${2:-|}
cd $DIR
for x in * ; do
[ -d $DIR/$x ] &&  echo "$SPACING\`-{"$x && $0 $DIR/$x "$SPACING  " 
done
I think it will keep the spaces (not on a *nix box, but I think I may have already done similar in dev)

EDIT ... needed to cd to $DIR for this way to work

I also have an efficient recursive space replace script in the dev version if you are interested, but I don't keep files with spaces around, so not well tested - just 1-2 sub-directories or so. It only requires mv (not sed awk or grep as others do - it uses only the shell's internal substring manipulation - resulting in ~1000% speed improvement)

... right now I don't have a net connection at home so I'm trying to work on a dialog/Xdialog wrapper, so that scripts work independent of X/console (I think goingnuts may have done some work on this already?)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

2lss
Posts: 225
Joined: Sun 20 Sep 2009, 23:54

#33 Post by 2lss »

technosaurus wrote:as an alternative you can simply use

Code: Select all

DIR=${1:-`pwd`}
SPACING=${2:-|}
cd $DIR
for x in * ; do
[ -d $DIR/$x ] &&  echo "$SPACING\`-{"$x && $0 $DIR/$x "$SPACING  " 
done
I think it will keep the spaces (not on a *nix box, but I think I may have already done similar in dev)

EDIT ... needed to cd to $DIR for this way to work
I gave this a try. Unfortunately there is still a problem. When the file or directory is evaluated by [ -d $DIR/$x ] if there is a space the test will throw an error. Quoting $DIR/$x causes the test to always fail. I think cd $DIR needs double quotes as well.

I will give the dev version a shot and see how it works.

EDIT: Which version is the dev version?
technosaurus wrote: It only requires mv (not sed awk or grep as others do - it uses only the shell's internal substring manipulation - resulting in ~1000% speed improvement)
Thats pretty awesome. I never put much thought into the performance aspect of shell scripting.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#34 Post by technosaurus »

I'll have to upload a new DEV version, but I need to transfer it to my droid or go find a hotspot somewhere (it may be a day or 2). My wife has our Altel internet stuff with her in Illinois.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#35 Post by technosaurus »

Just revisiting this to see if anyone is interested in something along these lines in the next generation of Puppy.

another random snippet.

Code: Select all

lsof(){
	for pid in /proc/[0-9]*; do
		exe=$(readlink $pid/exe)
		for fd in $pid/fd/*; do
			fd=$(readlink $fd)
			[ ${#fd} -lt 55 ] && printf " %-55s  %s\n" "$fd" "$exe" || \
			printf " %s ... %s  %s\n" ${fd:0:20} ${fd:$((${#fd}-30)):30} "$exe"
		done
	done |sort -u
}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

#36 Post by sc0ttman »

technosaurus wrote:Just revisiting this to see if anyone is interested in something along these lines in the next generation of Puppy.

another random snippet.

Code: Select all

lsof(){
	for pid in /proc/[0-9]*; do 
		for fd in $pid/fd/*; do
			fd=$(readlink $fd)
			[ ${#fd} -lt 55 ] && printf " %-55s  %s\n" "$fd" $(readlink $pid/exe) || \
			printf " %s ... %s  %s\n" ${fd:0:20} ${fd:$((${#fd}-30)):30} $(readlink $pid/exe)
		done
	done |sort -u
}
In puppy4 (maybe later versions) there was a 'pup4funcs' script or something ... it was sourced by many other scripts but never of much use to me in my scripts...

.. what would be *awesome* would be a directory of useful/repeatedly used scripts & funcs in /etc/* or /usr/lib/puppy/, that could be included in other stuff by puppy devs (and puppy itself, in its system scripts etc) .. this would reduce coding time, reduce redundant code in puppy userspace, reduce duplicate code, ease maintaining important system functions and features, and make it easier for newbies to start coding useful, safe, reliable apps of their own...

.. example funcs would include a lot of the funcs you wrote in bashbox

.. One reason to do this would be that Barry wrote pupdialog - then goingnuts, and myself, and others, also coded X/no-X UIs ... So a little library we could *all* use for this would be great..

For another (weak) example, we could have a file, /usr/lib/puppy/gtkdialog/pfselect

So, in any other script or app, we could call up a file selecter thingy, with only:

. /usr/lib/puppy/gtkdialog/pfselect
FILE=pfselect($WIDTH $HEIGHT $TITLE)
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#37 Post by technosaurus »

I was thinking about doing a rewrite so that more functions are split out into useful, generic functions. Zigbert did this to some extent, but I want to extend the traditional dialogs to actual "dialogs" (text2speech and voice recognition via espeak and pocket_sphinx_continuous) which is easier to do in a more structured approach. For example when a gtkdialog item list is generated, a corresponding voice menu is generated (if enabled) I'm considering extending the html generation with a httpd cgi backend as an alternative to gtkdialog (we could then use any browser for dialogs including netsurf-framebuffer/dillo or operate it headless/remotely).
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#38 Post by amigo »

Thanks for the 'lsof'. Too bad about the calls to readlink and sort. Of course the sort is probably not needed anyway. Can you think of how to implement readlink with only shell builtins?
As for the sort, I have a shell-only sort routine, or even better a uniq_no_sort routine. bash-only though as it uses arrays.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#39 Post by technosaurus »

@amigo - readlink can be ~like this:
http://stackoverflow.com/a/1116890/1162141
and a simple sort:
http://stackoverflow.com/a/16578322/1162141
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#40 Post by goingnuts »

sc0ttman: Fully support your thoughts. Might be difficult though - as we tend to focus on/understand best/use... our own X/no-X UIs. The X part explodes in favorite flavors - Xdialog, GTKdialog(1/2/3), yafsplash, xmessage etc. Need a very easy to use abstraction layer and/or a narrow selection of toolkits in use...

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#41 Post by amigo »

@techno HeHe, I saw this: 'If you think this is appalling, check out BashTrix' in the sort thread. Hopefully the sort offered there is better than my proof-of-concept... It might compare favorably to the slim 'sort' in the thread if cut down to a similar size. All those things in BashTrix are necessarily long beacuse of handling some of the normal command-line options. And they are still longer because I coded them so they *might* be easier to read.

As for the thread about readlink, the suggested script is using readlink. I was asking about implementing readlink itself in shell.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#42 Post by technosaurus »

The dialog/Xdialog/whiptail/kdialog interface is pretty well standardized (though, could use a few patches to make them more compatible). I could write a matching espeak/sphinx voice-dialog wrapper that can be used as a fairly straightforward template for other backends like yad or gtkdialog.

Edit: to make readlink a "builtin", just edit http://git.busybox.net/busybox/tree/inc ... lets.src.h accordingly

@Amigo - I think Bashtrix is great btw... Just figured that anyone who thought my little sort function was abysmal would be overwhelmed by Bashtrix.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#43 Post by technosaurus »

Code: Select all

sortline(){
for x in $@;do
    [ ! "$FIRST" ] && FIRST=t && set --
    i=0
    while [ $i -le $# ];do
        [ $x -lt $((${@:$((i+1)):1})) ] && break || i=$((i+1))
    done
    set -- ${@:1:$i}  $x   ${@:$((i+1)):$(($#-$i))}
done
echo $@
}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#44 Post by amigo »

This should fix the spaces-in-names problem for dir_tree:

Code: Select all

#!/bin/sh
DIR=$1
[ $2 ] && SPACING=$2 || SPACING="|"
for x in * ; do
[ -d "$DIR/$x" ] &&  echo "$SPACING\`-{"$x && $0 $DIR/$x "$SPACING  "
done
And this:
"'for' splits the input by line"
is not correct. 'for' splits according to the IFS, which by default is 'space tab end-of-line'. I know the comments are old, but felt it would be good to correct it -for posterity...

techno, Can you explain what FIRST is?

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#45 Post by technosaurus »

FIRST is poorly named, it should be something like ARGS_RESET

Code: Select all

[ ! "$FIRST" ] && FIRST=t && set -- 
resets $@ to "" on the first iteration (note that this is after the original $@ has been passed to the for loop), on subsequent runs $@ is modified by inserting the current iteration's value into $@ at the sorted location.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply