LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-06-2005, 12:04 PM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,428

Rep: Reputation: 53
Script to repeat commands


Hi folks,

I have to run 20+ commands repeatedly whenever turning on a PC as follows;

$ su
$ password
# command 1
# command 2
# command 3
/mnt/directoy-A (popup, if correct then run)
# command 4
# command 5
etc.

I'm prepard to write a simple script to do the job. Please advise how to start? With Shell or Perl? TIA

B.R.
satimis
 
Old 08-06-2005, 12:35 PM   #2
linuxLuser
Member
 
Registered: Jan 2005
Distribution: Gentoo
Posts: 111

Rep: Reputation: 16
Use BASH script

Use a shell script, something like:

Code:
#!/bin/bash
# 
# This is a comment line. Describe what the script is doing in English here
# 

command_1
command_2
command_3
# /mnt/directoy -A (popup, if correct then run) -- hmm, not sure what you mean here
command_4
command_5
# etc.
That should be about it. All you do is type in the command and it's parameters in the script. Each new line is a new command. Make sure that the first line in the text file is "#!/bin/bash" and that's it. Since this'll be a startup script, you won't need to su to run a root-owned-and-operated command (as on startup, init has control of everything anyways). Test the script out as root and when it works to perfection, copy it over to the init scripts directory and then make a symbolic link:
Code:
# cp myscript.sh /etc/rc.d/init.d/myscript
# cd /etc/rc.d/rc3.d
# ln -s S99myscript ../init.d/myscript
or something like that. It really depends on your distribution. Then you'll have it run on startup.
Now, if you need this to run at log in, it'll be a bit different. You'll want to put the script somewhere in the user's home directory and then edit your .bashrc file and add
Code:
~/myscript.sh
to it. Plus you'll need to put the "su" command in there. You'll have to interactively supply the password, as there's no easy way to have the script send the root password (plus, it's a really, really stupid idea to have a script execute commands as root. Major security risk).

One more thing, you'll have to
Code:
# chmod ug+x myscript.sh
in order for the script to work.

Hope that helps. Need more info, use Google to find out more info on bash scripting.

-- the dUdeMan
 
Old 08-07-2005, 11:44 AM   #3
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,428

Original Poster
Rep: Reputation: 53
Re: Use BASH script

Hi linuxLuser,

Tks for your detail advice and link.

Comment on following script would be appreciated.
Code:
#!/bin/bash

command-1
command-2
command-3
   if [REPLY="/mnt/lfs" ]; then
   command-4
else
   exit
command-5
command-6
etc.

fi
1) If the output of command-2 is "/mnt/lfs" then continue executing command-4. Otherwise exit the shell (stop running the script and exit).

2) Commencing from command-4 onwards, if there is an output, then confirmation is required. Please advise how to add such confirmation to each commands. TIA

B.R.
satimis
 
Old 08-07-2005, 11:48 AM   #4
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,428

Original Poster
Rep: Reputation: 53
Re: Re: Use BASH script

Hi linuxLuser,

Tks for your detail advice and link.

Comment on following script would be appreciated.
Code:
#!/bin/bash

command-1
command-2
command-3
   if [REPLY="/mnt/lfs" ]; then
   command-4
else
   exit
command-5
command-6
etc.

fi
1) If the output of command-2 is "/mnt/lfs" then continue executing command-4. Otherwise exit the shell (stop running the script and exit).

2) Commencing from command-4 onwards, if there is an output, then confirmation is required. Please advise how to add such confirmation to each commands. TIA

B.R.
satimis
 
Old 08-07-2005, 12:41 PM   #5
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
Code:
#!/bin/bash

command-1
OUTPUT=`command-2` #this line sets the output of command-2 to a varaible called OUTPUT
command-3
   if [ $OUTPUT = "/mnt/lfs" ]; then
   command-4
else
   exit
command-5
command-6
etc.

fi
also bash is sensitive to white space. these ways will not work
Code:
if [ $OUTPUT ="/mnt/lfs" ]; then
if [$OUTPUT = "/mnt/lfs" ]; then
but when you set a varible you must not have any whitespace
 
Old 08-07-2005, 11:00 PM   #6
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,428

Original Poster
Rep: Reputation: 53
Hi shanenin,

Tks for your advice.

<snip>

1)
Only command-3 should have output = /mnt/lfs. All commands should have no output. If there is any I need to confirm the output before continue to next command

2)
All commands don't carry number. It is only for easy reference/understanding I marked the command with a number

3)
All
Code:
   OUTPUT=$(command-x)
   if ["$OUTPUT"="yes" ]; then
   command-y
else
   exit
are repeated here. Is there a shorter way avoiding having a long script

Hereunder is the script. Please comment. TIA
Code:
#!/bin/bash

command-1
   OUTPUT=$(command-1)
   if ["$OUTPUT"="yes" ]; then
   command-2
else
   exit
command-2
   OUTPUT=$(command-2)
   if ["$OUTPUT"="yes" ]; then
   command-3
else
   exit
command-3
   OUTPUT=$(command-3)
   if ["$OUTPUT"="/mnt/lfs" ]; then
   command-4
else
   exit
command-4
   OUTPUT=$(command-5)
   if ["$OUTPUT"="yes" ]; then
   command-5
else
   exit
command-5
   OUTPUT=$(command-5)
   if ["$OUTPUT"="yes" ]; then
   command-6
else
   exit
command-6
   OUTPUT=$(command-6)
   if ["$OUTPUT"="yes" ]; then
   command-6
else
   exit
etc.

fi
Tks

B.R.
satimis
 
Old 08-08-2005, 10:24 PM   #7
linuxLuser
Member
 
Registered: Jan 2005
Distribution: Gentoo
Posts: 111

Rep: Reputation: 16
I don't even know if this'll work and I can't test it out at the moment. But I think it should. I can only think of doing this with nested if-statements. If this were C/C++, I'd probably use something to the effect of function pointers. But I don't know. This bit o' script doesn't seem all that long to me, actually.
Code:
#!/bin/bash
# 
# myscript
# 
if [ "yes" = $(command-1) ]; then
  if [ "yes" = $(command-2) ]; then
    if [ "yes" = $(command-3) ]; then
      if [ "/mnt/lfs" = $(command-4) ]; then
        if [ "yes" = $(command-5) ]; then
          if [ "yes" = $(command-6) ]; then
            echo "All commands completed sucessfully"
          else
            echo "myscript: Exit on command-6" > /dev/stderr
            exit
          fi
        else
          echo "myscript: Exit on command-5" > /dev/stderr
          exit
        fi
      else
        echo "myscript: Exit on command-4" > /dev/stderr
        exit
      fi
    else
      echo "myscript: Exit on command-3" > /dev/stderr
      exit
    fi
  else
    echo "myscript: Exit on command-2" > /dev/stderr
    exit
  fi
else
  echo "myscript: Exit on command-1" > /dev/stderr
  exit
fi
I'm going to go try this out on a computer soon, so I'll see if this actually works.

I hope that's what you're looking fer.

-- the dudemaN daVE
 
Old 08-10-2005, 03:55 PM   #8
linuxLuser
Member
 
Registered: Jan 2005
Distribution: Gentoo
Posts: 111

Rep: Reputation: 16
One thing I found out was that the if [ "yes" = $(command-1) ]; then should instead read if [ "yes" = "$(command-1)" ]; then (quotes around the $(command-1) part).

I was able to make some tests with this. I made my own script that calls several other scripts, and it has the same format as what I put above and it works well. I really don't see another shorter way.

-- the dUDeman DavE
 
Old 08-12-2005, 05:03 AM   #9
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,428

Original Poster
Rep: Reputation: 53
Hi linuxLuser,

Tks for your advice.

Quote:
I don't even know if this'll work and I can't test it out at the moment. But I think it should. I can only think of doing this with nested if-statements. If this were C/C++, I'd probably use something to the effect of function pointers. But I don't know. This bit o' script doesn't seem all that long to me, actually.
Code:
#!/bin/bash
# 
# myscript
# 
if [ "yes" = $(command-1) ]; then
  if [ "yes" = $(command-2) ]; then
    if [ "yes" = $(command-3) ]; then
      if [ "/mnt/lfs" = $(command-4) ]; then
        if [ "yes" = $(command-5) ]; then
          if [ "yes" = $(command-6) ]; then
            echo "All commands completed sucessfully"
          else
            echo "myscript: Exit on command-6" > /dev/stderr
            exit
          fi
        else
          echo "myscript: Exit on command-5" > /dev/stderr
          exit
        fi
      else
        echo "myscript: Exit on command-4" > /dev/stderr
        exit
      fi
    else
      echo "myscript: Exit on command-3" > /dev/stderr
      exit
    fi
  else
    echo "myscript: Exit on command-2" > /dev/stderr
    exit
  fi
else
  echo "myscript: Exit on command-1" > /dev/stderr
  exit
fi
I'm going to go try this out on a computer soon, so I'll see if this actually works.

I hope that's what you're looking fer.
Sorry I'm not vey clear of the abovementioned script

Whether
1) You meant "myscript" being
Code:
command-1
command-2
command-3
   if [REPLY="/mnt/lfs" ]; then
   command-4
else
   exit
command-5
command-6
etc.

fi
to replace;
Code:
command-1
command-2
etc.
with
Code:
full_command_line ;
full_command_line ;
etc.
OR alternative as
Code:
cmd1="full_command_line"
cmd2="full_command_line"
etc.
(Remarm: I use cm1, cmd2 to replce command-1 and command-2 etc. )

Please advise. TIA

B.R.
satimis
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
console commands as script? rooman Linux - Newbie 14 02-02-2006 08:49 PM
How do I script these commands? johnnybhoy67 Linux - General 6 10-21-2005 10:53 AM
Commands/Shell script ?? paraiso Linux - Newbie 11 04-21-2005 11:58 AM
commands behind a bash here script onurbi Linux - Software 3 11-14-2003 08:38 AM
run 2 commands in a script ddpicard Linux - General 10 06-13-2003 05:50 PM


All times are GMT -5. The time now is 03:03 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