SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-06-18 23:46:54

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

@script

allow me to start you off with a little history.

i used to be (and still occasionally am) active online in a MOO (which, for the uninitiated, is an object oriented MUD, so in full an Multi User Dungeon Object Oriented - a MOO! - and for all good reasons it is also the sound a cow makes)
these things were originally intended to be for online multiplayer text-games or some shit, but in my case it was filled with my friends and future friends to be, and was essentially a big private chatroom, only with medieval aliases (it is called 'Medieval' and originally themed that way).
in this MOO-environment, you could script all your own stuff (if you had the perms and quota). so, i went wild with making scripts for every conceivable way i could hit, kick, bash, kill, impale or gruesomely mutilate someone (this was all in text, so it was more funny than offensive, and we were among friends)
in this MOO, scripts were called 'verbs', and you could make a new script with the command '@verb crunchbang'. followed by '@edit crunchbang', it would open up in an editor and you'd be making a verb that could be the command 'crunchbang rhowaldt', and the verb would output (for all users in the room to see) 'rhowaldt crunchbangs himself!'
doing a lot of editing, the process of building a verb then editing it became quite tedious: 2 commands for 1 thing!
so, someone made @@verb, which would do @verb followed by @edit.

with bash scripting, each time i make a new script i do
'touch script'
'chmod +x script'
'gedit script &'

so, i figured, let me build a Linux @@verb script, like i had it in the MOO! and so i did. here it is for y'all to see and marvel at and by all means use. i can imagine someone else finding this handy. it is called @script.

#!/bin/bash

# @script will perform a repetitive task for you.
# it will make a new script, make it executable for you,
# then open it in gedit.

LOCATION=`pwd`
PATH=""
TOUCH="/usr/bin/touch"
CHMOD="/bin/chmod"
EDITOR="/usr/bin/gedit"

if [ $# -lt 1 ]; then
    echo "Use '@script <scriptname> [<location>]'."
    exit
fi
if [ $2 ]; then
    if [ ! -d "$2" ]; then
        echo "Location does not exist."
        exit
    else
        LOCATION="$2"
    fi
fi

PATH="$LOCATION/$1"
$TOUCH $PATH
$CHMOD +x $PATH
$EDITOR $PATH

for old time's sake, i've also added these two to my .bashrc:

# from the MOO-days
alias @verb="@script"
alias @edit="gedit"

by the way, if you are wondering why i put the full path to 'pwd', 'touch' and 'gedit' there, it is because for some reason i could not get it working just by name. if someone could help me out on that one, i'd be grateful.

Last edited by rhowaldt (2011-06-19 00:11:42)

Offline

Be excellent to each other!

#2 2011-08-12 13:30:32

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,300

Re: @script

You have to specify the full path to binaries because you use PATH as a script local variable, which obscures your PATH variable, which is the list of directories the system has to search to find executables.

The following version works OK:

#!/bin/bash

_LOCATION=`pwd`
_PATH=""
_TOUCH="touch"
_CHMOD="chmod"
_EDITOR="edit"

if [ $# -lt 1 ]; then
    echo "Use '@script <scriptname> [<location>]'."
    exit
fi
if [ $2 ]; then
    if [ ! -d "$2" ]; then
        echo "Location does not exist."
        exit 1
    else
        _LOCATION="$2"
    fi
fi

_PATH="$_LOCATION/$1"
$_TOUCH $_PATH
$_CHMOD +x $_PATH
$_EDITOR $_PATH

Note the _ in front of script local variables!
Note 2: 'edit' is an alias to emacsclient.
Cheers smile

Last edited by xaos52 (2011-08-12 13:32:07)

Online

#3 2011-08-13 13:37:38

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: @script

@xaos52: thanks for the info man, appreciate it! this will help me in future endeavors.

Last edited by rhowaldt (2011-09-07 20:04:14)

Offline

#4 2012-03-12 23:42:59

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

I'm obviously doing something wrong as I can't get it to work.

I've copied the script into a new file with Geany and made it executable, I've placed the resultant script in ~/bin.

In the Terminal when I type '@script' I get the message:

use '@script <scriptname> [<location>]'

So I tried:

@script new [~/bin]

but all I get is the message 'location does not exist.'

Could you put out of my misery, please? lol


#! Waldorf - 64bit - Xfce

Offline

#5 2012-03-13 00:12:38

2ManyDogs
dv#!
From: elsewhere
Registered: 2011-11-22
Posts: 1,346

Re: @script

@rhowaldt will probably come along soon, but until he does, try

@script new ~/bin

or just

@script new

(no brackets around the destination) -- it looks like he's saying the destination is an optional argument, and if you don't specify it the new script will be placed in the directory where you are running @script (that's the return value of `pwd`).

Last edited by 2ManyDogs (2012-03-13 00:17:46)


Be eggsalad to each other.

Offline

#6 2012-03-13 10:00:18

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: @script

^ exactly, thanks for explain Dog! smile

i figured i wouldn't have to explain the brackets, as they're a pretty common syntax in manpages and the like. yes, the location is optional. for me, i even forgot that feature was there. i just use

@script myawesomenewscript

Offline

#7 2012-03-13 14:44:09

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

Thanks guys, i was obviously taking it too literally, not being that familiar with scripts etc. wink


#! Waldorf - 64bit - Xfce

Offline

#8 2012-03-13 15:36:49

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: @script

^ no problem man! now that you have obtained '@script', you can start getting more familiar with scripts! smile

Offline

#9 2012-03-13 15:40:09

2ManyDogs
dv#!
From: elsewhere
Registered: 2011-11-22
Posts: 1,346

Re: @script

First exercise, go through @script line-by-line and figure out exactly how it works smile

And rhowaldt, I just have to say -- script-writing scripts are bloat smile


Be eggsalad to each other.

Offline

#10 2012-03-13 16:08:55

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

2ManyDogs wrote:

And rhowaldt, I just have to say -- script-writing scripts are bloat

Perhaps we could have a script to launch the script writting script? lol

No offence meant. big_smile


#! Waldorf - 64bit - Xfce

Offline

#11 2012-03-13 16:29:27

2ManyDogs
dv#!
From: elsewhere
Registered: 2011-11-22
Posts: 1,346

Re: @script

^ No, then we'd need a GUI front end for the script-writing script-launcher... smile


Be eggsalad to each other.

Offline

#12 2012-03-13 16:49:23

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

^ and GUI's are bad, especially for the minimalist, hence the use of scripts I suppose. wink


#! Waldorf - 64bit - Xfce

Offline

#13 2012-03-13 22:10:10

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: @script

'whatever you do, it is probably bloat'

Offline

#14 2012-03-14 22:44:49

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

I get the following when i try running the @script:

kris@kris-inspiron-1720:~/documents$ @script newscript
Warning: unknown mime-type for "/home/kris/documents/newscript" -- using "application/octet-stream"
Error: no "edit" mailcap rules found for type "application/octet-stream"
kris@kris-inspiron-1720:~/documents$ 

It creates a new text document in ~/documents but does not open it for editing nor does it make it executable.


#! Waldorf - 64bit - Xfce

Offline

#15 2012-03-14 22:50:54

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: @script

^ it uses Gedit... do you have that? or are you on the latest Statler with Geany as an editor? in that last case, open up the code and replace 'gedit' with 'geany' (only problem is can think of here... never seen this error)

Offline

#16 2012-03-14 22:56:15

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

Should i be using the script in your original post or the version in post 2 by xaos52?

When i try your original i don't seem to get anything but when i use the one by xaos52 i get what i posted previously.


#! Waldorf - 64bit - Xfce

Offline

#17 2012-03-14 22:59:04

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: @script

here's the one i'm using currently. if you indeed use Geany as your editor, replace the 'EDITOR' variable in the start of the script.

#!/bin/bash

# @script will perform a repetitive task for you.
# it will make a new script, make it executable for you,
# then open it in gedit.

LOCATION=`pwd`
PATH=""
TOUCH="/usr/bin/touch"
CHMOD="/bin/chmod"
EDITOR="/usr/bin/gedit"

if [ $# -lt 1 ]; then
    echo "Use '@script <scriptname> [<location>]'."
    exit
fi
if [ $2 ]; then
    if [ ! -d "$2" ]; then
        echo "Location does not exist."
        exit
    else
        LOCATION="$2"
    fi
fi

PATH="$LOCATION/$1"
$TOUCH $PATH
$CHMOD +x $PATH
$EDITOR $PATH &

Offline

#18 2012-03-14 23:12:56

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

^excellent, that works a charm.  I do indeed use Geany so changed it as you suggested.
I did change it in the previous version I was using but only the one you just posted seemed to work for me.


#! Waldorf - 64bit - Xfce

Offline

#19 2012-03-15 00:01:43

damo
#! Die Hard
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 653

Re: @script

You could also do this...

For a shell script (eg @shscript) add this before the last line

echo "#!/bin/bash" > $1
echo "##" >> $1

For a python script (eg @pyscript) add

echo "#!/usr/bin/env python" > $1
echo "##" >> $1

etc...

Nice idea for a time-saver BTW smile

PS How do all you terminalistas keep track of all your scripts and shell commands?


Artwork at deviantArt; Iceweasel Personas; GDM #! Themes;
SLiM #! Themes

Offline

#20 2012-03-15 08:34:38

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: @script

^ my scripts are in ~/scripts (with subdirectories for C, Python, and whatever else i'm gonna be embarking on). aliases are in ~/.rhowaldt_bashrc

i just remember what scripts i have because i use them quite regularly. if i don't remember, i navigate to my scripts-dir and see what the name of that particular script was.

shell-commands: see my signature, the 'system' link. i just do 'system ref' and get a list of handy commands for various functions.

Offline

#21 2012-03-15 12:42:52

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

@rhowaldt..  How do execute your scripts?

I save mine in the 'bin' folder as below:

~/bin/@script

and then I execute the script from the terminal.  However if I save the script in another folder say:

~/scripts/@scripts

then i cannot execute the script from the terminal anymore. sad


#! Waldorf - 64bit - Xfce

Offline

#22 2012-03-15 13:05:03

2ManyDogs
dv#!
From: elsewhere
Registered: 2011-11-22
Posts: 1,346

Re: @script

Is ~/scripts/@scripts in your PATH?


Be eggsalad to each other.

Offline

#23 2012-03-15 13:07:16

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

^ apologies for the noob in me, but how do I tell?


#! Waldorf - 64bit - Xfce

Offline

#24 2012-03-15 13:24:26

2ManyDogs
dv#!
From: elsewhere
Registered: 2011-11-22
Posts: 1,346

Re: @script

Sorry, I should have explained that. If you don't know, it probably isn't, but to check do this:

echo $PATH

Then if it's not there, open ~/.bashrc, and add this at the end:

#add @scripts and bin folders to path
PATH=$PATH:~/scripts/@scripts:~/bin
export PATH

If that doesn't work we can try something else.


Be eggsalad to each other.

Offline

Be excellent to each other!

#25 2012-03-15 13:34:02

kri5
#! Die Hard
From: L.G.C. UK
Registered: 2011-11-10
Posts: 568

Re: @script

^ It wasn't in the path so I added the code to the .bashrc file, unfortunately it still says command not found.


#! Waldorf - 64bit - Xfce

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo