1

i've created simple bash script that do the following :

#!/usr/bin/env bash
cf ssh "$1"

When I run the command line from the CLI like cf ssh myapp its running as expected, but when I run the script like

. myscript.sh myapp 

I got error: App not found

I dont understand what is the difference, I've provided the app name after I invoke the script , what could be missing here ?

update

when I run the script with the following its working, any idea why the "$1" is not working ...

#!/usr/bin/env bash
cf ssh myapp
Jenny Hilton
  • 1,297
  • 7
  • 21
  • 40
  • Why do you source (`.`) the script? Is `cf` an alias, a command, or something else, and where and how is it installed/defined? The error message looks like something from `cf` which is probably not a programming problem as such. – tripleee Mar 26 '17 at 13:24
  • I dont use `cf` but it looks like you will get in trouble when opening a TTY and try to connect to an other tty via ssh. Did you try `cf ssh` without any programm name, so you can see if it works at all. You could also try to change the instance for your app via `cf -i`, but i am not sure it this will help. – Mario Mar 26 '17 at 13:29
  • @tripleee - what do you mean by source (.) the script ? how can I call it diffrent than . myscript app name ? I run the script from the same path that I was able to run the command cf ssh myapp (which works) I just want to create some script that run it... – Jenny Hilton Mar 26 '17 at 13:31
  • @suleiman - yes when I put in the script exactly the command I need like cf ssh myapp its working ... the only diff is that in the script I want to pass a parameter "$1" ... – Jenny Hilton Mar 26 '17 at 13:35
  • when you create a script you can name it like you want. Then you will need to make it executeable `mod +x filename`. You can call Scripts via `./scriptname` or you add a bin folder to home and add some rights to it too. Alternative use the folder `/usr/local/bin` to store scripts and similar files. The files will get called without any point or shlash in front of it then, By the way. the normal bash declaration is `#!/bin/bash` without env in it. – Mario Mar 26 '17 at 13:39
  • The `env` is certainly harmless and a good idea if your script needs to be portable to systems where Bash is not in `/bin`. – tripleee Mar 26 '17 at 14:17
  • Sourcing is explained here: http://stackoverflow.com/questions/13786499/what-is-the-difference-between-sh-and-source – tripleee Mar 26 '17 at 14:19

1 Answers1

4

When you do this:

. myscript.sh myapp

You don't run the script, but you source the file named in the first argument. Sourcing means reading the file, so it's as if the lines in the file were typed on the command line. In your case what happens is this:

  • myscript.sh is treates as the file to source and the myapp argument is ignored.

  • This line is treated as a comment and skipped.

    #!/usr/bin/env bash
    
  • This line:

    cf ssh "$1"
    

    is read as it stands. "$1" takes the value of $1 in the calling shell. Possibly - most likely in your case - it's blank.

Now you should know why it works as expected when you source this version of your script:

#!/usr/bin/env bash
cf ssh myapp 

There's no $1 to resolve, so everything goes smoothly.

To run the script and be able to pass arguments to it, you need to make the file executable and then execute it (as opposed to sourcing). You can execute the script for example this way:

./script.bash arg1 arg2