0

I'm trying to find out how to use the command line to enter no more than three parameters into my program...
It current asks the user to say their username, name and password - which then puts the name, password and date/time of creation into a log file with their username as the doc file - as seen below

function savefile {
# Save to file
  if [ -e "$username".log ]; then
    echo "Username already exists, returning you to the menu."
    sleep 2
    clear
      menu
  else
    echo "$fullname" >> "$username".log
    echo "$password" >> "$username".log
      curdate=$(date +'%d/%m/%Y %H:%M:%S')
    echo "$curdate" >> "$username".log
  echo "Creating your account"
    sleep 2
    clear
  echo "Account created"
  echo
    afterBasic
  fi 
}  

Wondering if you guys knows how to do this from the command line? I know i have to use

sh 1 2 3

But that's about it...

Here is the same thing written in Batch, if that also helps...

set OP_username=%1
set OP_fullname=%2
set OP_password=%3
    if [%3]==[%9] goto 1
echo %OP_fullname% >> %OP_username%.log
echo %OP_password% >> %OP_username%.log
echo %date% %time% >> %OP_username%.log
jww
  • 97,681
  • 90
  • 411
  • 885
Badja
  • 857
  • 1
  • 8
  • 33

2 Answers2

3

To access a command line argument you use the $ sign. So to access the first command line variable you write $1 and so on. So setting the variable OP_username to equal the first argument you can write OP_username=$1.

To run your program with the arguments you write saveFile 1 2 3 on the command line. saveFile is a function and not a bash script. If you want to run it as a bash script save it in a .sh file. Running the .sh file will define the function which you can then call as above. Another option is to not use a function but rather define your code in a .sh file let's say called saveFile.sh. Then you could run sh saveFile.sh 1 2 3. So your code would be:

saveFile.sh/function

# Save to file
username=$1
fullname=$2
password=$3
if [ -e "$username".log ]; then
  echo "Username already exists, returning you to the menu."
  sleep 2
  clear
  menu
else
  echo "$fullname" >> "$username".log
  echo "$password" >> "$username".log
  curdate=$(date +'%d/%m/%Y %H:%M:%S')
  echo "$curdate" >> "$username".log
  echo "Creating your account"
  sleep 2
  clear
  echo "Account created"
  echo
  afterBasic
fi 
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
  • Thanks for the help. Unfortunately, the program must be run from just one script, how would I do this? – Badja May 09 '15 at 23:32
  • I'm not sure I understand. This code can be written in a stand alone sh file or as a function in a bigger script. – Benjy Kessler May 09 '15 at 23:34
  • Well when I run the code as normal, it's telling me an account already exists, no matter what data I put in – Badja May 09 '15 at 23:39
  • If you are having another problem in your script you should ask it in a separate question. Have you managed to get the input parameters to work? – Benjy Kessler May 09 '15 at 23:40
  • If I put them at the top of the doc WITH the file writing code, yeah. But when nested in the function, neither the command line nor the actual program work :/ – Badja May 09 '15 at 23:41
  • Can you write how you are calling the function? – Benjy Kessler May 09 '15 at 23:46
  • It's a bit janky. Programsays hi; calls username function, then calls fullname function, then calls password function, then calls savefile function as you see above – Badja May 09 '15 at 23:52
  • See http://stackoverflow.com/questions/8818119/linux-how-can-i-run-a-function-from-a-script-in-command-line it might help clarify things. – Benjy Kessler May 10 '15 at 00:09
0

The command line parameters are $1, $2, $3, and so on in (ba)sh scripts.

So you could use

#!/bin/bash
OP_username=$1
OP_fullname=$2
OP_password=$3
# ...
Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136