1

Suppose I have a bash script called foo.sh

#!/bin/bash
echo "Type your name"
read personname
if["$personname" == "kevin"]; then
    echo "Your name is kevin"
    exit 1
fi

Now, I give it global access as cp foo.sh /usr/bin/foo

So, I find it difficult now to call the script and pass the argument at the same time. I can only do

bash foo
john

instead of doing

bash foo john 
jub0bs
  • 60,866
  • 25
  • 183
  • 186
robue-a7119895
  • 816
  • 2
  • 11
  • 31
  • 3
    Just to save you some time later: you need more whitespace in your `if` statement: `if [ "$personname" = "$kevin" ]; then`. (And don't use `==` inside `[...]`, even if `bash` allows it. Use `=`, or switch to `[[ ... ]]` instead.) – chepner Nov 14 '14 at 20:16
  • 1
    `read` reads from stdin. Command-line arguments are not passed on stdin. – Charles Duffy Nov 14 '14 at 20:26
  • Ohh, good to know those. I wander why we have to use `=` instead of `==` – robue-a7119895 Nov 14 '14 at 20:27
  • By virtue of using `bash foo`, that tells me you have not set the execute permission bit on /usr/bin/foo. You should simply be able to invoke the script with `foo john`. To set the execute bit, either use octal permissions `chmod 0755 /usr/bin/foo` or `chmod ug+x /usr/bin/foo` – David C. Rankin Nov 14 '14 at 20:28
  • 1
    @Contax, because `==` isn't specified by the POSIX sh standard, so only some shells support it. Using `=` in comparisons, on the other hand, is part of the standard -- so any POSIX-compliant shell will work with that syntax. – Charles Duffy Nov 14 '14 at 20:28
  • @DavidC.Rankin No, the issue was on how to call the script and pass it an argument at the same time. – robue-a7119895 Nov 14 '14 at 20:29
  • I get that, that is why I left a **comment** instead of an **answer**... – David C. Rankin Nov 14 '14 at 20:30
  • By the way -- I'm disappointed, when asking this, that you ignored feedback you were given on your prior question about the same code. – Charles Duffy Nov 14 '14 at 20:36
  • @CharlesDuffy No, I just copy-pasted the question from there. All the feedbacks are taken with high regard. – robue-a7119895 Nov 14 '14 at 20:37

3 Answers3

1

Arguments end up in the variables $1 - $9. $0 stores the name the script was called with (useful if you have multiple symlinks poinitng to one script and it should react differnetly for each, for example). If you ahve more than 9 arguments, you can get access to the other ones by calling shift or enclosing the arugment number in brackets (${10} and so on).

In addition to that, you should give yourself (and possibly everybody) execution priviliges on that script, so you can call it by just using "foo" without calling bash manually - that's what you ahve the shebang line for.

Johannes H.
  • 5,875
  • 1
  • 20
  • 40
1

If you want to receive a parameter, you have to modify it that way:

#!/bin/bash
personname=$1
if [ "$personname" = "kevin" ] ; then
    echo "Your name is kevin"
    exit 1
fi

Now you could call the script with one parameter foo john.

jherran
  • 3,337
  • 8
  • 37
  • 54
  • Too much whitespace on the assignment right now. Needs to be `personname=$1`, not `personname = $1`; the latter invokes `personname` as a command, with `=` its first argument. – Charles Duffy Nov 14 '14 at 20:25
1

You can batch feed things read through redirection of standard input in most cases.

bash foo <<HERE
john
HERE

In this case I used what is called a HERE file which begins with the two less thans followed by a tag that will be used to declare the end of input.

Another example might be to use echo and pipe to the program:

echo "kevin" | bash foo

Likewise you could do a simple redirect from a name stored in a file or other resource that can read using stdin.

  • WHile this solves the problem, it doesn't answer the question. You are not using arguments here, you're just filling STDIN with values before starting the program. – Johannes H. Nov 14 '14 at 20:18