LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices

Rate this Entry

More (Generic) Tools

Posted 12-20-2012 at 08:08 PM by rainbowsally
Updated 12-20-2012 at 08:48 PM by rainbowsally (bum code)

More (Generic) Tools

Behind the scenes, some of the more complex tools are getting incorporated into lq-qt-mc2. Should have an upload in a day or two. lq-find will kock your socks off.. now tuned up and ready for the big time. :-) v. 3.0.14 coming soon.

Today's Features
  • Create a shortcut with a shortcut icon, directories - OK!
  • Another new.<utility> using the mc2 extras demo (env-replace)
  • Revisiting the utility to create a self extractor using base64

Ever tried to make a shortcut (link to a url) for a directory name? This utility takes the path and creates a shortcut that LOOKS like a shortcut.

This can be symlinked into your ~/bin and will show with the 'new.listall' we created recently, or whatever, if you don't think you need it.


file: new.shortcut
purpose: utility (executable) to create links that look like links, directories = ok
Code:
#!/bin/sh

usage()
{
  echo "Need a full path to a directory or a file"
}

if [ "$1" == "" ]; then
usage; exit 1
fi

if [ "$1" == "--help" ]; then
usage; exit 0
fi

HERE=$PWD

ICONTYPE=link

APPPATH=`readlink -f "$0"`
THERE=`dirname "$APPPATH"`

# These are the only two variables needed to modify 
# in the template.

export URL="$1"
export ICON=$THERE/types/$ICONTYPE.png

SHORTCUTNAME=`basename "$URL"`

rm -f "$HERE/$SHORTCUTNAME" # in case it already exists as a symlink
env-replace $THERE/shortcut.tplt > "$SHORTCUTNAME"
 
# some linuxes require the shortcuts to be executable
# you may even have to make it an xdg-* script by adding this line
# at the top. 
#   "#!/xdg-open"
# or something like that

if test -f "$SHORTCUTNAME"; then
  if chmod +x "$SHORTCUTNAME"; then 
    echo "See your new desktop shortcut file named 
  \"$SHORTCUTNAME\"
  in the current folder. -rs"
  fi
else
  echo "Failed to create desktop shortcut file named
\"$SHORTCUTNAME\"
in the current folder. -rs"
fi

file: shortcut.tplt
purpose: source file for use with 'env-replace' to generate a shortcut
Code:
[Desktop Entry]
Icon=%{ICON}%
Type=Link
URL[$e]=file://%{URL}%


file: types.sfxz
purpose: utility (executable) to create the types folder and it's 'link.png' icon
Code:
#!/bin/sh
# base64 self extractor
# file: types

filename=types

# don't allow user to click on the file
if ! tty >/dev/null; then
  xmessage -center "
  $(basename $0)
  must be run in a terminal 
  "
  exit
fi

# create and/or clear out tmp dir
mkdir -p $HOME/tmp/sfxz
rm -rf $HOME/tmp/sfxz/*
base64 -d << _BEOF >$HOME/tmp/sfxz/$filename.xz
/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4Cf/B+ldABcLywfxyKlieeZi1jxkpnRkCHtV551YfyBd
VWGxxYqa+tgoPN7XwYahv9Ynvq0yQ+C2Qg/FahL/+khuSkON70ujOyIYE0++u5bpiNtByR2MtWod
QbG8j/zbTMyiiovDRXTBF8jIpGNDrstg/dEOVa9vuawFpA3ixJ1lVmcDfTS5sApZZu769CsJgDVX
Om+ZlAX+tdeJA6pSPCeuDi1Wqmu/g6vOFIouc5BXAJeNoA0yd1dBj6d5vIkSykxrG5XoSRy/5lE/
K+FxWXH2kFmZYG759bM62xkVG3TAPptLyTWn6WkK3jQjlsLOm04N6/S+6QAsLWES4BAwdUHQ69xb
mVNvLpw1Q9huUluY9HUGqcrbUXPADWqoMWMx6s2NT/KGZ9SMztRixUw+JkwhrIjQMLwd9poEfcBB
SkN3q5x8NmZsczI0gsUTUY458sckCV5UCUIcXK/4h1aPenrPxgJyYIQp1gghUoSt9rkpt8pkC7GU
oVYny/NG3lZuF5z7N+QrRoVfrQjqefDW8ZoDHcLYAS9xVoVoQ/S3QZZOmFyhipCnpzUZ7LWxkpSm
JO0Ry7AwUksRVFWEvQZuiF1rmSkcSG4IAMfCI7ifNtVJa/F9z2zn51wZ8sAgweZcOSUveL8NwFoX
+SdKEWaU42vXXAv16MKtDRM0pIr57xgYyTcL3pKbC9whEdpCvO+whVsxa4xk+/O1DsdsMv7VqdQi
Gcs4yMGnlp/bgMbzxW0g5n0Y5PmqHfOH1S1/aavWBms0fxwajhk181l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g/VQb1WNsQ/VUFWmFSZ4E02oy9Iy
/PG1bPHONA4Uj0/7KzSGhh3oYdoKP3LweBBku8QeEB5g58O3W0+NYtUMMOGCpQvZasfKt/xYgPpL
OMcJtqALrLh99T2DcjtIRsUKSDete/4eugDsqOLMyWVd1iKM0XS2wrn8Qq9IVKg95TTVJwS+S2Yu
wYwXk+wp/8nNzHYesDocHBEjOhfFx8AzR6ss6dUgpryubnQFd0nzUYJzK0UcSMeUxaWuLQ0nOZVH
12d9Xeb/E6rpUZ4gLqPOIM/6IYS/Zf8ViJYQJsKfvJ11IAmdOVXBj7JQOMvy6G493nMe+1gNkaNt
IoXeUED+Pm11JJkqjX6plCFibXB4P4Jo8eVpab1pesc206ROpUcuibVYPngXH4L1ux1FKqu7Ior7
nQCTMWXOHgd8dvYYAr4BHm3FH7GnHe375eUacbze1E1PW+n7fjdYWVtTKwpzedf/NVIe7C0JYCjU
pTiuCYpNmz78qnWcqkaPv/8BxuSGci9ItYRLAEag8lmEseyQ5Go+HTSPkzB6Ju58rflciG6gUaml
PPqYTECmSB9lQZw7IUeMKEFL/B/Mwhfm1j1ug81mvBe564pZzZ+fTpsGPQfUSR9+/qIrsCEE7lEw
om0ubwb76Q//bKkr6DBJlu2VkEhEiJrG6uiojKg5YdF1ZCXzsMrK+Za1lBVtA7cy5dOjIVgXHPaN
Veo1lxjFmWEhxuxY3nbfRK5qLWUaWhk4b8aajlBGVwxdz2uqk8lHP8dbo6ZX3A8KtxLP51Ek0uAA
RSYYUT9ztmVjbVR5YeFKYmDsftI/8dA1MuTIIg8+Fs8cCmMlJYUFuhiKd6uGmBFzBglBaJqA4+Aq
D+bfAAAAAACWeLgAWUUt8gABhRCAUAAAv14tbLHEZ/sCAAAAAARZWg==
_BEOF

(cd $HOME/tmp/sfxz && tar -xaf types.xz)
rm -f $HOME/tmp/sfxz/types.xz
 
cat << _BEOF > $HOME/tmp/sfxz/post-extract
#!/bin/sh

is_yes() # returns OK if first char is Y or y
{
  local key=`echo $1 | cut -b1 | sed 's/y/Y/; /Y/!d'`
  [ "$key" == 'Y' ] && return 0 # true
  return 1 # false
}

if [ -e types ]; then
  printf "
  Overwrite existing file(s)? [N/y]:"
  read key
  if ! is_yes $key ;then
  echo "Aborting.."
    exit 0
  fi
fi

mv $HOME/tmp/sfxz/types .
_BEOF

sh $HOME/tmp/sfxz/post-extract
rm -rf $HOME/tmp/sfxz/*
rmdir $HOME/tmp/sfxz 2>/dev/null || true
rmdir $HOME/tmp 2>/dev/null || true
-------------------------
We created this self extractor a while back but I'm not sure we created the new.sfxz utility. So here it is in all it's glory. And it'a another new.<tool> and this too will show in new.listall if you symlink it into your HOME/bin folder.


You can make it use tar.gz instead, obviously, but xz compresses so nice that these base64 jobbies end up about the same size as a gzipped tarball that does not self extract.

file: /HOME/bin/new.sfxz (link to the actual file)
purpose: utility (executable)
Code:
#!/bin/sh
# base64 encoded xz-lzma compressed self extracting 
# file creator
# requires tar, xz (lzma) compression

############################
# usage check

if [ "$1" == "" ] || [ "$1" == "--help" ]; then
echo "
  Usage new.sfxz <filename | dirname>
  Creates self-extracting file named <filename>.sfxz
  Requires xz (lzma) at both ends of operation.

  Uses tar so expect similar path issues.

  If a post extraction script named 'post.sh' exists
  it will be run at extraction time.  It should assume the 
  extracted file(s) are in \$HOME/tmp/sfxz when it runs.

  The default post extraction action is to move the 
  extracted file or dir from the \$HOME/tmp/sfxz directory 
  to the current working directory.
 "
exit
fi

filename=`basename $1`
srcdir=`dirname $1`
post="post-extract"


############################
# compress xz

if ! tar -caf $filename.xz $srcdir/$filename >/dev/null; then
  echo "
  Can't convert '$srcdir/$filename' to self extractor"
  exit 1
fi


############################
# write output file header

cat << _EOF > $filename.sfxz
#!/bin/sh
# base64 self extractor
# file: $filename

filename=$filename

# don't allow user to click on the file
if ! tty >/dev/null; then
  xmessage -center "
  \$(basename \$0)
  must be run in a terminal 
  "
  exit
fi

# create and/or clear out tmp dir
mkdir -p \$HOME/tmp/sfxz
rm -rf \$HOME/tmp/sfxz/*
base64 -d << _BEOF >\$HOME/tmp/sfxz/\$filename.xz
_EOF


############################
# write output file footer
# with renamed post extraction
# script

base64 $filename.xz >> $filename.sfxz
echo "_BEOF

(cd \$HOME/tmp/sfxz && tar -xaf $filename.xz)
rm -f \$HOME/tmp/sfxz/$filename.xz
 " >> $filename.sfxz

if [ -e post.sh ]; then
  cp post.sh $post
else
cat << _EOF > $post
#!/bin/sh

is_yes() # returns OK if first char is Y or y
{
  local key=\`echo \$1 | cut -b1 | sed 's/y/Y/; /Y/!d'\`
  [ "\$key" == 'Y' ] && return 0 # true
  return 1 # false
}

if [ -e $filename ]; then
  printf "
  Overwrite existing file(s)? [N/y]:"
  read key
  if ! is_yes \$key ;then
  echo "Aborting.."
    exit 0
  fi
fi

mv \$HOME/tmp/sfxz/$filename .
_EOF
fi


echo "cat << _BEOF > \$HOME/tmp/sfxz/$post" >> $filename.sfxz
cat $post >> $filename.sfxz
echo "_BEOF

sh \$HOME/tmp/sfxz/$post
rm -rf \$HOME/tmp/sfxz/*
rmdir \$HOME/tmp/sfxz 2>/dev/null || true
rmdir \$HOME/tmp 2>/dev/null || true
 " >> $filename.sfxz


############################
# cleanup

rm -f $filename.xz
rm -f $post

############################
# make executable

chmod +x $filename.sfxz
The new.shortcut utility will create directory icons for files. So if you want to also use this for files, you'll have to copy or make an icon for files (or other types) and change the 'TYPE' in the new.shortcut code appropriately to select the proper png file from the types subfolder.

Have you got 'new.main' to not clobber an existing main.cpp if it's got another name yet? It's easy! :-)

Note: Some of these uploads are of buggy or dumbed down versions before I fix them myself.

They work but not great (yet). But working code, even limping code, is easier to fix than stuff that doesn't work at all. There's nothing like frustration to motivate invention! ;-) And that's why I do that (with notes about what needs to be improved).

So I have better stuff in my folders than I upload here sometimes.

BUT I may not have better stuff than you have in yours. And that's cool!

Very cool...

- The Computer Mad Science Team

:-)
Posted in Uncategorized
Views 543 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 07:11 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration