Syntax for executing a script using Git Bash on Windows 10 pro/docker/ddev - bash

I am executing the following command:
wget -O -https://raw.githubusercontent.com/drud/ddev/master/scripts/windows_ddev_nfs_setup.sh | bash
And I get the following:
wget: missing URL
Usage: wget [OPTION]... [URL]...
Looks as if I have the syntax wrong, but I have been searching to find out how to fix this and haven't found an answer.
I checked the command on shellcheck.net and learned the following:
SC2148: Tips depend on target shell and yours is unknown. Add a shebang.
That sounds great, but I don't (yet) know what it means.

You are using -O, which means that the next argument is the output file. Hence,
-https://raw.githubusercontent.com/drud/ddev/master/scripts/windows_ddev_nfs_setup.sh
is the name of your output file, and there is no URL in your command.
Since you are piping the result of wget into bash, you don't need an output file, I think.

Related

Is it possible to make a .bat Bash hybrid?

In cmd, it is possible to use Linux commands with the ubuntu or bash commands, but they are very fickle. In batch, it is also possible to make a VBScript-batch hybrid, which got me thinking, is it possible to make a Bash-batch hybrid? Besides being a tongue-twister, I feel that Bash-batch scripts may be really useful.
What I have tried so far
So far I tried using the empty bash and ubuntu commands alone since they switch the normal command-prompt to the Ubuntu/Bash shell, but even if you put commands after the ubuntu/bash they wouldn't show or do anything.
After I tried that, I tried using the ubuntu -run command, but like I said earlier, it’s really fickle and inconsistent on what things work and what things don't. It is less inconsistent when you pipe things into it, but it still usually doesn't work.
I looked here since it seemed like it would answer my question and I tried it, but it didn't work since it required another program (I think).
I also looked to this and I guess it failed miserably, but interesting concept.
What I've gotten from all of my research is that most people think when this is mentioned of a file that could be run either as a .bat file or as .sh shell file instead of my goal, to make a file that runs both batch and Bash commands in the same instance.
What I want this for relates to my other question where I am trying to hash a string instead of a file in cmd, and you could do it with a Bash command, but I would still like to keep the file as a batch file.
Sure you can use Bash in batch, assuming it is available. Just use the command bash -c 'cmd', where cmd is the command that you want to run in Bash.
The following batch line pipes the Hello to cat -A command that prints it including the invisible symbols:
echo Hello | bash -c "cat -A"
Compare the output with the result of the version completely written in Bash:
bash -c "echo Hello | cat -A"
They will slightly differ!

What is mean that `grep -m 1 ` command in UNIX

I googled this command but there was not.
grep -m 1 "\[{" xxx.txt > xxx.txt
However I typed this command, error didn't occured.
Actually, there was not also result of this command.
Please anyone explain me this command's working?
This command reads from and writes to the same file, but not in a left-to-right fashion. In fact > xxx.txt runs first, emptying the file before the grep command starts reading it. Therefore there is no output. You can fix this by storing the result in a temporary file and then renaming that file to the original name.
PS: Some commands, like sed, have an output file option which works around this issue by not relying on shell redirects.

How to append the ouptut of more than a command to a filename through a redirect in bash?

I'm trying to create a file containing the list of packages installed in the system at an exact time. We're trying to compare different hosts at different times to check some sources.list tests so I need the file name to include the hostname and the date.
I know I can use something like:
dpkg -l > dpkg-list-`hostname`.txt
or
dpkg -l > dpkg-list-$(hostname).txt
The same with the date command. The problem is when I try to combine both variables:
dpkg -l > dpkg-list-$(hostname)$(date).txt
-bash: dpkg-list-$(hostname)$(date).txt: ambiguous redirect
I've tried using all combinations of the commands above, bash keeps complaining about an ambiguous redirect.
Now, I know I could easily create a bash script that loads both variables and appends them to the file, but this is more a proof of concept so I'd like to learn how to do this in a one-liner.
Try narrowing the format of date output using the following:
dpkg -l > dpkg-list-$(hostname)$(date +'%Y%m%d').txt
gives:
dpkg-list-myhost20160506.txt
Like #123 commented, use double quotes
dpkg -l > "dpkg-list-$(hostname)$(date).txt"

How do I execute commands with quotation marks in bash?

Basically, I have a file (say.sh) which uses an api to save and play text to speech mp3 files. The api uses the URL: http://api.voicerss.org/?key=keygoeshere&src=TEXT_GOES_HERE&hl=en-gb&c=mp3&r=1&f=32khz_8bit_stereo
The script (bellow) uses wget to get the file. In theory, this code should work, but it doesn't. If I echo the wget command, it returns a working command, that successfully runs, but as soon as I remove the echo, it gets confused by the quotation marks and stops working.
#!/bin/bash
TA="http://api.voicerss.org/?key=MY_KEY_IS_HERE&src="
TB="&hl=en-gb&c=mp3&r=1&f=32khz_8bit_stereo"
wget -O example.mp3 \"$TA$#$TB\"
omxplayer example.mp3
If anybody here knows how to fix this, it would be very helpful. Thanks!
EDIT: To run the command I have tried sh say.sh Text here and sh say.sh "Text here". Neither of which work -_-
i don't know that you need to escape any quotes for the wget command, but i think if variables are appended next to each other, you can try using {}:
#!/bin/bash
TA="http://api.voicerss.org/?key=MY_KEY_IS_HERE&src="
TB="&hl=en-gb&c=mp3&r=1&f=32khz_8bit_stereo"
wget -O example.mp3 "${TA}${TB}"
omxplayer example.mp3
give that a try and let us know how it goes.
Thanks for the responses! I managed to fix this by adding the "${TA}${#}${TB}" and using \" to declare a special character to get:
#!/bin/bash
TA="http://api.voicerss.org/?key=keygoeshere&src=\""
TB="\"&hl=en-gb&c=mp3&r=1&f=32khz_8bit_stereo"
wget -O example.mp3 "${TA}${#}${TB}"
omxplayer example.mp3

Simple shell script doesn't work like command line?

I'm trying to write a script that contains this
screen -S demo -d -m which should start a new screen session named demo and detach it.
Putting screen -S demo -d -m in the command line works.
If I put it in a file named boot.sh, and run it ./boot.sh I get
Error: Unknown option m
Why does this work in the command line but not as a shell script?
This file was transferred from windows and had ctrl-M characters.
Running "screen" on my Linux machine, a bad option (Screen version 4.00.03jw4 (FAU) 2-May-06) gives the error,
Error: Unknown option -z"
while your description includes no dash before the offending option. I'd check that the characters in your script file are what you expect them to be. There are many characters that look like a dash but which are not.
cat -v boot.sh
may show something interesting as it'll show codes for non-ascii characters.
This may seem a little like the "make sure your printer is plugged in" kind of help, but anyway:
have you tried to check if the screen you're invoking from the script is the same as the one invoked from the command line ?
I'm thinking you may change the PATH variable inside your script somewhere and perhaps screen from the script would be something else (a different version, perhaps ?).

Resources