Arguments help needed for scripting!!!!

C

csalcedo1994

Guest
This is what I have to do I did steps 1-6 already i need help in 7-10 I JUST need to know the commands for creating the argument to do that.

  1. Using the terminal, move your last script from its current location to a folder you will create under your home directory (~) called bin
  2. Rename that file in your new bin folder to mybackup
  3. Add bin to your path
  4. To do this, type export PATH=$PATH:~/bin
  5. Before proceeding, ensure that your script uses absolute paths rather than relative paths
  6. Make sure to take note of the commands you just used in steps 1-4
  7. Modify the script file now stored in your bin folder so that it accepts an arguement
  8. The new arguement will be a path for a folder you want backed up
  9. Your script will replace each instance of /var with that arguement
  10. You should be able to run your script from anywhere in Linux and have it backup to a set location
  11. Alter your filename so that your path becomes a part of it, along with the date
 


OP
G

GrumpyOldMan

Guest
Here's a hint:

Every argument you pass comes in as a variable - $1 is the first, $2 is the second, etc.
 
OP
C

csalcedo1994

Guest
Ok but how do I execute them and how do I put them in a script
 
OP
C

csalcedo1994

Guest
Still don't understand like you see on top im try to copy a folder to a backup folder but I need to do it as a new argument as the first argument but idk what commands i need i read them but it still doesnt make sense
 
OP
S

SHarper

Guest
csalcedo1994, what GrumpyOldMan was trying to say is:
first of all) when you call a script at a prompt like this
$script.sh argument1 argument2
or better example:
$emaillogs.sh /var/log/messages /var/log/messages.old

the arguments are referenced inside your script as $1 for argument1 or /var/log/messages and $2 for argument2 or /var/log/messages.old. So, inside your script, if you want to reference the argument, just use $1, $2, $3, etc.

secondly) to learn more about linux shell scripting, follow the links that he provided in his reply.
 
OP
C

csalcedo1994

Guest
Ok let me see if I get it for example if im putting a command in the script i first put $1 and when i run the script i put .\$1Script or how you see im taking a linux class in college and i dont know any of this im new to it and my professor just give us the assignments so i need to know like can you show me an example
 
OP
S

SHarper

Guest
you do not use $1 when you run the script. You use it inside the script while you are writing it. It is how you reference the values passed into the script when run. so, at the command line you would run:
.\script arg1 arg2
and inside the script, to use the value passed in arg1, you would reference $1 and arg2 is $2
 
OP
R

rstanley

Guest
Ok let me see if I get it for example if im putting a command in the script i first put $1 and when i run the script i put .\$1Script or how you see im taking a linux class in college and i dont know any of this im new to it and my professor just give us the assignments so i need to know like can you show me an example

Script args-ex:
Code:
#!/bin/bash
  echo "Script Name:" $0
  echo "Argument #1:" $1
  echo "Argument #2:" $2
  echo "Argument #3:" $3
Output:
Code:
$ ./args-ex red white blue
Script Name: ./args-ex
Argument #1: red
Argument #2: white
Argument #3: blue

I hope this helps.
 
OP
C

csalcedo1994

Guest
Yes it did this is what I was looking for Thank rstanley
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Linux.org Hosting Donations
Consider making a donation

Staff online


Top