bash for loop in python crashed [duplicate] - bash

I am a beginner of bash. I encounter a problem like this:
$ "make -p"
when I type the above in bash command line, there is nothing to happen, no error, no result msg.
I have searched double quotes syntax of bash in many websites. All of these materials give similar interpretation as below:
https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
and give examples like:
echo "argument"
I do not find something like "echo argument". Moreover, I find a strange difference between bash command line and bash scripts.
If I type a non-existing command in command line:
$ "holy shit"
$ "look that"
there is nothing to happen. But if I type it in bash scripts:
#!/bin/bash
"holy shit"
"look that"
and execute this script, an error msg will be throw out:
$ ./myshell
./myshell: line 2: holy shit: command not found
./myshell: line 3: look that: command not found
Would someone can help give a detailed interpretation about the effect of double quotes when they enclosed the whole command?
Why there is no output in command-line?
Why it is different between command line and scripts?

If you enter a command foo, the shell searches the directories listed in your PATH variable until it finds a command of this name. If there is none, you get the error message command not found.
If you enter a command, which contains at least one slash - for example ./foo or foo/bar -, the shell does not search the PATH, but assumes that you have already entered the correct path to your command. If it does not exist, you get the error message No such file or directory.
In your case,
"cd home"
searches for a file with name cd home somewhere along your PATH, but there is no file of this name, and you get command not found. If you enter
"cd /home"
the shell bypasses PATH-search and assumes, that there exists a directory named cd (i.e. the 3 letters c,d,space) in your current directory, and below it a file named home, with x-bit set. There is no such file (and no such directory) on your system, and you get the error message No such file or directory.
If you are in the mood of experimenting around, you could try the following:
mydir="cd "
mkdir "$mydir"
echo "echo Hello Stranger" >"$mydir/home"
chmod +x "$mydir/home"
"cd /home"
This should print Hello Stranger. Pay attention that in the assignment to mydir, there must be a single space between the cd and the closing quote.

The double quotes mean it is a string. You can do something like:
echo "Hello everybody"
either at the command line or the shell. Sometimes when people put stuff in quotes. you are supposed to replace what is in quotes with your own variable (removing the quotes), and sometimes people put quotes around the whole command you are supposed to type to show the what exactly you should type. For your example of "make -p" just type it without the quotes and it should work in both the command line and as a script.

Related

How to pass variables with special characters into a bash script when called from terminal

Hello all I have a program running on a linux OS that allows me to call a bash script upon a trigger (such as a file transfer). I will run something like:
/usr/bin/env bash -c "updatelog.sh '${filesize}' '${filename}'"
and the scripts job is to update the log file with the file name and file size. But if I pass in a file name with a single quote in its file name then it will break the script and give an error saying "Unexpected EOF while looking for matching `''"
I realize that a file name with a single quote is making the calling command an invalid one since the single quote is messing with the command itself. However I don't want to sanitize the variables if I can help it cause I would like my log to have the exact file name being displayed to easier cross reference it later. Is this possible or is sanitizing the only option here?
Thanks very much for your time and assistance.
Sanitization is absolutely not needed.
The simplest solution, assuming your script is properly executable (has +x permissions and a valid shebang line), is:
./updatelog.sh "$filesize" "$filename"
If for some reason you must use the bash -c, use single quotes instead of double quotes surrounding your code, and keep your data out-of-band from that code:
bash -c 'updatelog.sh "$#"' 'updatelog' "$filesize" "$filename"
Note that only updatelog.sh "$#" is inside the -c argument and parsed as code, and that this string is in single quotes, passed through without any changes whatsoever.
Following it are your arguments $0, $1 and $2; $0 is used when printing error messages, while $1 and $2 go into the list of arguments -- aka $# -- passed through to updatelog.sh.

Bash Script Path Containing Space

I just have this simple script :
#!/bin/bash
mainDir="EVE-NG\ Repos"
mega-ls ahmedrafat#debugz-it.com:$mainDir
But I got this error :
[API:err: 08:40:34] Couldn't find "ahmedrafat#debugz-it.com:EVE-NG\ Repos"
When I enter the command natively in bash like this :
mega-ls ahmedrafat#debugz-it.com:EVE-NG\ Repos
It works, but inside the script, it doesn't.
Try typing this in at the shell:
mega-ls "ahmedrafat#debugz-it.com:EVE-NG Repos"
Does that clear things up? Inside quotes you don't need to escape spaces (and it doesn't work, sending \char instead).
Note that this is required all the way:
Myvar="this and that"
echo "$Myvar" #with quotes to get 1 arg

How to use gitbash with file names that contain ASCII exclamation point characters like !0-MyFolder

I need to use gitbash on MS-Windows with file folder names that contain an exclamation point as the first characters, like "!0-MyProjectFolder" (without the quotation marks. I use the exclamation point to sort Microsoft Windows files to the top, since Windows does not provide a way to index and force sort order of files and folders.
Gitbash keeps giving me error messages:
I've tried several syntaxes already:
$ cd "!0-Projects-WIP"
bash: !0: event not found
$ cd "\!0-Projects-WIP"
bash: \!0: event not found
$ cd !0-Projects-WIP
bash: !0: event not found
Be clear that I am NOT parsing a string like '/New.*desktop.*is/!d' in the StackOverflow posting at How to address error "bash: !d': event not found" in Bash command substitution
VNCServerAndDisplayNumber="$(echo "${VNCServerResponse}" \
| sed '/New.*desktop.*is/!d' | awk -F" desktop is " '{print $2}')"
I am passing the directory name !0-Projects-WIP to GitBash, so that I can change into the directory named !0-Projects-WIP. I am not intending to do double-quoting or history expansion. If the exclamation point in the folder name appears to be a history expansion directive, then that is not the intended result. The ! must be escaped in my case so that it is read correctly as part of the folder name, and the shell command "cd" interprets it as a string.
I realize now that !0-Projects-WIP is probably a bad name to a Unix shell parser because it directs the command line parser to do something that was not my intention, but for the MS-Windows command line shell there is no confusion.
I got it to work with just a single escape character in front of the string:
$ cd \!0-Projects-WIP
rlysa#domainname MINGW64 /c/users/rlysak01/desktop/!0-Projects-WIP (master)
$
Simple solution. No quotation marks needed on the folder name string.

Bash wildcard working in command line but not script

I've a basic ls command I'm running in my command line $ ls [root]/*, from which I can see the contents of my $root directory. But, when I run the same command in a script, I see this error: ls: [root]/*: No such file or directory. I'm going to paste the script below, but is someone able to tell why the command is running fine in the command line but not the script? Thank you.
#! /bin/bash
root="[root]"
ls "$root/*"
edit:
I tracked down the problem. The wildcard should not be inside the double quotes unless I'm looking for a file or directory with that name. The script below runs successfully.
#! /bin/bash
root="[root]"
ls "$root/"*
The best answer to my question was the edit I made to my question. I'll say it again.
I wanted to use an ls wildcard in a bash script and wrote this file:
#! /bin/bash
root="[root]"
ls "$root/*"
But, writing my ls this way looks for files or directories with the name *. In order to make use of the wildcard I need to leave * outside of the quotes. See the example below:
#! /bin/bash
root="[root]"
ls "$root/"*
note:
I'm using the square brackets [] in my question and answer contextually. The name of my directory isn't the literal string [root], it's something else, but the square brackets let the reader understand what's being said contextually.
But maybe a fake root path or name is better in the future.
There's a few issues here.
[root] is a shell globbing pattern that matches any of the three characters r, o or t. If you do ls [root] and you happen to have a directory or file called r, o or t, then that pattern would expand to the name of that file or directory.
To safely list the content of a directory whose real name is [root], you would need to quote the name, which brings us to the next point...
A shell globbing pattern does not expand in quotes. This means that "[root]/*" refers to something called *, literally, in the directory [root].
Your script would have to look like
#!/bin/sh
root='[root]'
ls "$root"/*
The existent answers do not solve the problem if you use complex patterns like this:
cp release/artifacts/${OS}/binary?(.exe) $DEST
Because it failed with:
line 6: syntax error near unexpected token `('
To solve that you should add the following option to the script:
#!/usr/bin/bash
shopt -s extglob

Shell scripting: new line command not found

I am trying to run my shell script from command line lets say;
my script looks like this:
#!bin/bash
echo hello
When try to run this source ./abcd.sh I get this error.
"' is not a typo you can run the following command to lookup the package that contains the binary:
command-not-found
: command not found
hello
"
Never seen this before something wrong with having a empty line before "echo hello" ? I was wondering if anyone else encountered something like this.
Along with the first line of your script being a comment, it sounds like your file has DOS line endings, and the carriage return is being treated as the command that isn't found. The error message sounds like something provided by a custom command_not_found_handle function (which I believe Ubuntu defines).
#!bin/bash
needs to be
#!/bin/bash
or wherever bash is installed (you can locate this by doing whereis bash).
Your program should work fine when invoked using bash, i.e., bash ./abcd.sh, but when executed directly ./abcd.sh then the hashbang line does matter because that is how the interpreter is located for the script contained in the executable file.
Try echo 'hello', within quotes. It looks like there is a newline between the echo command and hello and it is trying to run 'hello' as a command.
The hashbang line should be #!/bin/bash, but messing that up won't matter as it will interpret any line that starts with a hash as a comment.
Run script with debug option to see which line actually is failing:
bash -x abcd.sh
Note: in this case the Shebang line will be treated as a comment, so if the rest of your script is correct, it will execute correctly:
$ bash -x abcd.sh
+ echo hello
hello
Make sure your file does not have a BOM
I had the same problem when editing a script under Windows with Notepad++.
make sure to convert to "UTF-8 witout BOM".

Resources