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 Fri 18 Apr 2014, 09:03
All times are UTC - 4
 Forum index » Advanced Topics » Cutting edge
bashbox: multicall bash script
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 3 of 3 [45 Posts]   Goto page: Previous 1, 2, 3
Author Message
2lss

Joined: 20 Sep 2009
Posts: 225

PostPosted: Sun 13 Mar 2011, 23:13    Post subject:  

technosaurus wrote:
just save this into a script to test the dir_tree portion
Code:
#!/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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Mon 14 Mar 2011, 11:38    Post subject:  

as an alternative you can simply use
Code:
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?)

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
2lss

Joined: 20 Sep 2009
Posts: 225

PostPosted: Tue 15 Mar 2011, 02:44    Post subject:  

technosaurus wrote:
as an alternative you can simply use
Code:
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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 15 Mar 2011, 10:54    Post subject:  

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.
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Mon 18 Nov 2013, 20:56    Post subject:  

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

another random snippet.
Code:
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
}

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2363
Location: UK

PostPosted: Mon 18 Nov 2013, 21:09    Post subject:  

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:
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)

_________________
Akita Linux, VLC-GTK, Pup Search, Pup File Search
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Mon 18 Nov 2013, 23:30    Post subject:  

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).
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2167

PostPosted: Tue 19 Nov 2013, 05:20    Post subject:  

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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 19 Nov 2013, 12:47    Post subject:  

@amigo - readlink can be ~like this:
http://stackoverflow.com/a/1116890/1162141
and a simple sort:
http://stackoverflow.com/a/16578322/1162141

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
goingnuts

Joined: 07 Dec 2008
Posts: 777

PostPosted: Tue 19 Nov 2013, 12:55    Post subject:  

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...
Back to top
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 2167

PostPosted: Tue 19 Nov 2013, 13:50    Post subject:  

@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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 19 Nov 2013, 15:42    Post subject:  

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/include/applets.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.

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Tue 19 Nov 2013, 22:09    Post subject:  

Code:
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 $@
}

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2167

PostPosted: Wed 20 Nov 2013, 04:45    Post subject:  

This should fix the spaces-in-names problem for dir_tree:
Code:
#!/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?
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4134

PostPosted: Wed 20 Nov 2013, 10:27    Post subject:  

FIRST is poorly named, it should be something like ARGS_RESET
Code:
[ ! "$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.
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 3 of 3 [45 Posts]   Goto page: Previous 1, 2, 3
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Advanced Topics » Cutting edge
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.0844s ][ Queries: 12 (0.0065s) ][ GZIP on ]