LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories
User Name
Password
LinuxQuestions.org Member Success Stories Just spent four hours configuring your favorite program? Just figured out a Linux problem that has been stumping you for months?
Post your Linux Success Stories here.

Notices


View Poll Results: Do you find this script useful?
Excellent! It's the best thing since bash 2 33.33%
Not bad. I'm a hardcore user who likes RSI 4 66.67%
Useless. It seems incompatible with my rodent 0 0%
Voters: 6. You may not vote on this poll

Reply
  Search this Thread
Old 01-18-2004, 11:02 AM   #1
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Lightbulb Quick script for running repetitive commands


I just thought I'd post this incase anyone else found it useful. I wrote it mainly to save typing out users and hosts for ssh connections but left it so it could be used for running any sort of command.

All you need to do is put the floowing script into a file called "/usr/local/bin/go", or anywhere else in your path.
Code:
#!/bin/bash

if [ -r ~/.go ] && [ ! $1 ];then
  OLDIFS=$IFS
IFS="
"
  num=0
  choice=1
  while [ $choice -lt 1 ] || [ $choice -gt $((num-1)) ];do
    a=1
    num=1
    echo "          Please choose a command number below"
    for line in `cat ~/.go`;do
      if [ $a -eq 1 ];then
        command[$num]=$line
        a=2
      else
        echo "          $num) $line"
        a=1
        num=$((num+=1))
      fi
    done
    echo -n "          Enter your selection: "
    read choice
    echo
    IFS=$OLDIFS
    ${command[$choice]}
  done
else
  echo You do not appear to have a \~/.go config file or it is not readable
  echo \~/.go files should consist of 2 lines, the first being the command
  echo and the second being the description that is used by the go script.
  echo
  echo Version 1, Written by David Ross http://www.rossy.co.uk
fi
Then make it executable:
chmod 755 /usr/local/bin/go

You then need a basic configuration file called ".go" in your home directory. The file takes the structure:
Code:
command 1
Description of command 1
command 2
Description of command 2
and so on... You should end up with a file like
Code:
ssh root@server.mydomain.com
SSH as root to my server
ssh webmaster@server.mydomain.com
SSH as webmaster to my server
/home/user/backup.sh
Run backup script
I hope you find it useful .

Last edited by david_ross; 01-18-2004 at 11:07 AM.
 
Old 01-18-2004, 05:47 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Very nifty. I don't actually have a use for it at the moment, but I'll come up with one (or many) and find it very useful, I'm sure. It was cool to see and to try to figure out what was going on. Forgive me if I'm totally missing the boat or screwing stuff up but I made a mod for typos or other brain damage in blue (I need those kinds of error-checks because I make a lot of mitsakes.) Thanks for sharing! (I wish more people would post stuff like this.)
Code:
#!/bin/bash

if [ -r ~/.go ] && [ ! $1 ];then
  OLDIFS=$IFS
IFS="
"
  num=0
  choice=1
  while [ $choice -lt 1 ] || [ $choice -gt $((num-1)) ];do
    a=1
    num=1
    echo "          Please choose a command number below"
    for line in `cat ~/.go`;do
      if [ $a -eq 1 ];then
        command[$num]=$line
        a=2
      else
        echo "          $num) $line"
        a=1
        num=$((num+=1))
      fi
    done
    echo -n "          Enter your selection: "
    read choice
    if [ $choice -ge $num ] || [ $choice -eq 0 ];then
      echo "That is not an available option."
    else
      echo
      IFS=$OLDIFS
      ${command[$choice]}
    fi
  done
else
  echo You do not appear to have a \~/.go config file or it is not readable
  echo \~/.go files should consist of 2 lines, the first being the command
  echo and the second being the description that is used by the go script.
  echo
  echo Version 1, Written by David Ross http://www.rossy.co.uk
fi
 
Old 01-19-2004, 12:30 PM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Original Poster
Rep: Reputation: 79
Nice addition. I tend not to do that much error checking when I write something for myself but It's good to have - I'll update my local version too

digiot, if your interested in another useless script then here is another.
http://www.linuxquestions.org/questi...threadid=91333

Ok a quick edit. I've added a bit of colour coding just for the hell of it:
Code:
#!/bin/bash

if [ -r ~/.go ] && [ ! $1 ];then
  OLDIFS=$IFS
IFS="
"
  num=0
  choice=1
  while [ $choice -lt 1 ] || [ $choice -gt $((num-1)) ];do
    a=1
    num=1
    echo -e "\033[0;36m          Please choose a command number below:\033[0m"
    for line in `cat ~/.go`;do
      if [ $a -eq 1 ];then
        command[$num]=$line
        a=2
      else
        echo "            $num) $line"
        a=1
        num=$((num+=1))
      fi
    done
    echo -ne "\n\033[0;36m          Enter your selection:\033[0;33m "
    read choice
    if [ $choice -ge $num ] || [ $choice -eq 0 ];then
      echo -e "\033[0;31m          That is not an available option.\033[0m\n"
    else
      echo -e "\033[0m"
      IFS=$OLDIFS
      ${command[$choice]}
    fi
  done
else
  echo You do not appear to have a \~/.go config file or it is not readable
  echo \~/.go files should consist of 2 lines, the first being the command
  echo and the second being the description that is used by the go script.
  echo
  echo Version 1.0, Written by David Ross http://www.rossy.co.uk
  echo Version 1.1, Additional error checking by digiot
  echo Version 1.2, Colour coding by David Ross
fi

Last edited by david_ross; 01-19-2004 at 12:51 PM.
 
Old 01-19-2004, 08:36 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
The color's a nice touch. I really like it when color's used, partly because it looks good but mostly because it does make it easier to read when done right, like that.

Thanks for pointing me to that second script. That's a great example of potentially getting a *lot* done relative to the size of the script. I always like any sort of 'preservation/backup/times of disaster' script.

I'm very much a beginner and not very good at this. And for some reason I get as much or more out of checking out a 'live' script as reading a whole chapter of a tutorial. Educational and useful.
 
Old 01-25-2004, 09:12 AM   #5
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
Very good. I am going to use this . Thanks .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OBP commands Quick Reference Card ]un]ie Solaris / OpenSolaris 1 07-13-2010 11:58 AM
Running Commands blazted Linux - Newbie 6 11-13-2004 03:49 PM
Quick question: Learning commands Kami.JZ Linux - Newbie 4 10-24-2004 04:14 PM
running commands ./ ?? greendemon Linux - Newbie 4 08-04-2003 09:54 AM
Running commands linuxnewbie001 Linux - Newbie 6 08-01-2003 04:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories

All times are GMT -5. The time now is 09:03 PM.

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
Open Source Consulting | Domain Registration