LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to repeat commands (https://www.linuxquestions.org/questions/programming-9/script-to-repeat-commands-350575/)

satimis 08-06-2005 11:04 AM

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

linuxLuser 08-06-2005 11:35 AM

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

satimis 08-07-2005 10:44 AM

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

satimis 08-07-2005 10:48 AM

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

shanenin 08-07-2005 11:41 AM

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

satimis 08-07-2005 10:00 PM

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

linuxLuser 08-08-2005 09:24 PM

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

linuxLuser 08-10-2005 02:55 PM

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

satimis 08-12-2005 04:03 AM

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


All times are GMT -5. The time now is 07:46 PM.