1

I am a completely new user to UNIX shell programming. I want to know how I can pass a variable that is now a comment to another comment which is for getting a file from a website. For example:

I have made this variable as a command:

password1=$(.....)

and my new command is like this:

wget "http://.........?something&thecommandiscontinues"

I want to replace the word something with the output from my password1 command, so that I do not have to write a new script and manually enter it myself. Please help me with writing simply and giving me solution. I have been working on this problem several weeks, but not resolved yet. Thank you.

  • 1
    Are you sure it's not something like http://...?password=something&... where something is meant to be URI-encoded? – Stéphane Chazelas Mar 14 '17 at 16:46
  • 1
    Try (set -x; curl -GO --data-urlencode "=$password1" -d thecommandiscontinues http://........) – Stéphane Chazelas Mar 14 '17 at 16:50
  • No, set -x is so you can have a visual feedback of what command is being run. You need to replace ..... with the host name and path of the file (something like host.example.com/path/to/file without the trailing ?) thecommandiscontinues is the text after the & you have in your question! Add a -v option to curl to see what HTTP request is being made. And if that doesn't work, please tell in what way it doesn't work. – Stéphane Chazelas Mar 14 '17 at 16:59
  • $password1 contains spaces characters. If you had done printf '"%s"\n' "$password1", you would have seen them. You probably need to change the command that retrieves the password. – Stéphane Chazelas Mar 14 '17 at 17:20
  • Add another s/^ *//;s/ *$// to your sed command to remove leading an trailing space characters. Now what if the password contains space characters? It's common to have spaces in passwords. – Stéphane Chazelas Mar 14 '17 at 17:26
3

Replace something with ${password1}

  • 3
    @JIGIL, in what way does it not work? – Stéphane Chazelas Mar 14 '17 at 16:45
  • @JIGIL It does not make sense not to work. Also check your variable defininition . Try password="mypass" - remove $( ) – George Vasiliou Mar 14 '17 at 21:35
1

So if i did get it right you want to add what is in the variable password1 to the input of the command wget?

If this is the case try creating a fuction like this:

myFunction () {
    wget -arg "something[...]$password1"
}

You can call this function in your code using this:

myVariable=$(myFunction);

You will get the output of your command in that variable.

  • Should I write anything in the brackets after myFunction? – reader Mar 14 '17 at 16:08
  • I edited my post so i specify how to call the function:You can call this function in your code using this: myVariable=$(myFunction); You will get the output of your command in that variable. – Kingofkech Mar 14 '17 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.