SEARCH

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

You are not logged in.

#1 2012-09-20 13:22:17

marcdjay
New Member
Registered: 2012-09-20
Posts: 1

Bash script to create bash scripts

Hi all!

New #!'er and newish linux user here. Just starting to learn a bit more about bash scripting and I've found it tedious creating the file, opening it to edit, chmod'ing it then executing it with ./.

So, I decided to knock up a quick script that will create a /bin folder in my home dir then add it to the $PATH, take the 1st argument and create a file with that name, chmod it to allow execution and then open it up in your fave editor (yes im a nano man, what of it!)

Haven't tested it yet as I'm at work on my Windows box but thought I'd share it anyway and see what you think / show me where I've gone wrong etc smile

#!/bin/bash

#==========================================================================
# TITLE: mksh (MaKe SHell)
# AUTHOR: MarcDJay
#
# USAGE: mksh <scriptname>
# DESCRIPTION:  Creates a file with given argument, copies it to ~/bin,
#                makes the file executable and then opens it in your editor
#                of choice.
#==========================================================================

# Edit the following and replace with your favourite editor
EDITOR=changeme

# Checks to see if the command already exists, and if it does quits.
which $1 | $(ishere)
if $ISHERE [ != "" ]
    echo "Command already exists! Terminating..."
    echo ""
    sleep 1s
    exit
fi

# Check if ~/bin exists, and if not, create it & add to PATH
if [ ! -d "~/bin" ]; then
    mkdir ~/bin
    echo "PATH="$PATH:$HOME/bin"" >> ~/.profile
fi

# Creates the file to become the script
touch $HOME/bin/$1
echo "#!/bin/bash" >> $1

# Makes the file executable
chmod +x $HOME/bin/$1

# Opens file in your editor
$EDITOR $HOME/bin/$1
exit

Offline

Help fund CrunchBang, donate to the project!

#2 2012-09-27 19:40:57

wuxmedia
wookiee madclaw
From: Normal for Normandy
Registered: 2012-03-09
Posts: 842
Website

Re: Bash script to create bash scripts

I think you've just cracked the old chicken and egg scenario..!
great idea - might even start trying bash now.
thanks. smile

Offline

#3 2012-09-27 19:46:27

pidsley
#! Die Hard
Registered: 2012-05-23
Posts: 1,348

Offline

#4 2012-09-27 20:01:35

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

Re: Bash script to create bash scripts

^ it is indeed exactly the same principle.

nice script DJ Marc! smile makes a lot of sense to create this indeed.

Offline

#5 2012-09-28 17:29:55

stat
Member
Registered: 2012-06-15
Posts: 40

Re: Bash script to create bash scripts

Great idea. I have always used a template that I made in geany but often forget to open the template and eventually get pissed at myself because I finish the script and realize I left out the summary, etc.  have to open the template and copy it all back in etc.  A script to write scripts is just crazy enough that my addled brain will remember it.  As they said in Blazing Saddles "stand back, I think he's just crazy enough to do it".


Find something useful here? Help fund CrunchBang, donate to the project!

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