How to run sh file in GIT Bash - shell

Am trying to run physusr.sh file in GIT Bash in Windows. Am trying to set java home as below in physusr.sh file.
JAVA_HOME=C:/Program Files (x86)/IBM/WebSphere/AppServer
JAVA_EXE=$JAVA_HOME/bin/java
cd /H/US_L3/MLAdminBatchLocal/original
but am facing the error when I run the file GIT Bash
./physusr.sh: line 1: syntax error near unexpected token `('
./physusr.sh: line 1: `JAVA_HOME=C:/Program Files (x86)/IBM/WebSphere/AppServer'
I have tried using double quotes, back slash but I was getting no such file or directory error. How do I make this work. Should I run this sh file using any other tool.

Most probably the '(' bracket character of '(x86)' is causing the problem. When it executes the bash file it is maybe considering it as something else but not the path. So, to solve this, tell the executor that the whole thing is a path or we can say disable the different treatment of special characters like brackets, put the path inside single quotes.
So, change it to:
JAVA_HOME='C:/Program Files (x86)/IBM/WebSphere/AppServer'
JAVA_EXE=$JAVA_HOME/bin/java
cd /H/US_L3/MLAdminBatchLocal/original

Related

bash for loop in python crashed [duplicate]

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.

Error processing variables with special characters in bash script

I need help trying to find a solution to have a bash script be able to read file names with special characters. The user will start the script, but if the folder or the file has special characters, the script will fail or have an error. I have tried several options I found online, but I have not been able to make them work with the script.
The script is set up to take user input with the read command.
read -r -p "Enter directory name : " var1
If the user input is “accoutn&orders,” the script will fail due to the ‘&’ character as it won’t find the directory or file.
When the script looks for the file with specific extensions, the input folder name will be the path to copy the files to a different directory. The issue I am running into is that some of those files or directories have special characters, and the script cannot process the variables and cannot find the file when there are special characters.
The script uses a for loop to check every file in the directory, and if the file's name has a special character, it will fail the loop.
example file name:
file1#depot.rct
file2&logrecord.rct
cd $var1
ls: cannot access '/sharepool/comunityshare//'\''account.&.orders'\''': No such file or directory
line 141: cd: '/sharepool/comunityshare//'\''account.&.orders'\''': No such file or directory
I have tried using single quotes wrapping and bask slashes, but the variable is not readable.
Please note that I am not a coder or developer, I know some basic Linux commands, and I am trying to make this work while a better process is developed. I appreciate your help with this.
I was able to solve the issue using this line.
filename=$(echo "$filename" | sed 's/[&()#+*#!%^'\''^]/\\&/g')
That inserted a backslash if the variable had a special character.
account.&.orders to account.&.orders
Thank you for your help and support.

Overriding make variables containing spaces with MAKEFLAGS

I have a bash script to run my Makefile-based project through Include-What-You-Use (IWYU) which looks as follows:
#!/bin/bash
export MAKEFLAGS="-k CXX=iwyu -Xiwyu --transitive_includes_only -Xiwyu --mapping_file=qt5_4.imp"
build.sh
The build.sh script sets a couple of environment variables, prepares other parts of the build environment and will then eventually run make.
At first sight this seems to work and do its job. However closer inspection showed, that only CXX=iwyu is actually used in the build. The command line options for IWYU get dropped.
I tried various modifications of my call to fix this, however none seemed to solve the problem.
With
export MAKEFLAGS="-k CXX=iwyu\ -Xiwyu\ --transitive_includes_only\ -Xiwyu\ --mapping_file=qt5_4.imp"
the command line options are no longer dropped, but now suddenly -k seems to get dropped somewhere and my build (because of the missing -k) is terminated early with failure.
With
export MAKEFLAGS="-k CXX='iwyu -Xiwyu --transitive_includes_only -Xiwyu --mapping_file=qt5_4.imp'"
I'm flooded with /bin/sh: 1: Syntax error: Unterminated quoted string which looks like the ending ' is somehow dropped.
Are there any other ways I have to escape and/or quote the spaces in my export to fix this?
You can create a script (once?) to run iwyu — be careful, single quotes and double quotes are not interchangeable:
echo 'iwyu -Xiwyu --transitive_includes_only -Xiwyu --mapping_file=qt5_4.imp "$#"' > ./run.iwyu
chmod +x ./run.iwyu
and then run:
make -k CXX="$PWD/run.iwyu"
or:
export MAKEFLAGS="-k CXX=$PWD/run.iwyu"
This sidesteps the whole problem of spaces in the arguments. As shown, you specify the full path for the run.iwyu script just in case your make process changes directories. If you put the run.iwyu script in a directory on your PATH, you don't need to specify the full path to the script. You could prefix the command line in the script with exec if you like.

Azure DevOps bash script inline vs path not the same output

I tried to run a bash script through the bash script task inside Azure DevOps.
When my bash script was still small I always used the inline type and runned the code there. After my script became larger than 5000 characters I was forced to use the path type. But got some errors when using the path type:
After some debugging trying to find out where the hosting agent is fooling me.
If i use the same script of 5000 character with the inline type and with the path type it works with the inline type but not with the path type which got me thinking it's about my settings not with the code itself.
This is my setting with path type
Even when i give a path after all it's a path type in the "Script path" parameter it gave me the same error. Is there a setting I forget to set. Or do i do something wrong by just copy pasting the inline script inside a file ?
[EDIT]
The script that is running is: https://paste.ee/p/XGY7Z
Looks like a newline issue:
/home/vsts/work/1/s/CheckAlerts.sh: line 6: declare: `arrReportsFailed
': not a valid identifier
Note the newline in the middle of the error message. Similarly:
/home/vsts/work/1/s/CheckAlerts.sh: line 13: syntax error near unexpected token `$'do\r''
Note the \r.
bash doesn't cope with Windows-style (CRLF) line endings. Change your file to have Unix-style (LF) line endings. Enforce this with a .gitattributes:
*.sh text eol=lf

Running R script from Shell using CygWin: error "Rscript not found"

This is the first time I am trying to run the R file from CygWin terminal.
I have a file named linreg.R and I am in the same directory as file in CygWin terminal.
There is a shell script in the same directory that take in input linreg.R and another data.txt (located at some other place).
When I am running the bash with appropriate inputs, its again and again giving me the same error:
$ ./build_model_from_directory.sh linreg.R /workdir/workdir/prod_data_v.txt lm_try
./build_model_from_directory.sh: line 27: type: Rscript: not found
Rscript is needed for linreg.R. Exiting
When I put something like this:
$./build_model_from_directory.sh linreg.Rscript /workdir/workdir/prod_data_v.txt lm_try
Script assumes linreg.Rscript in same directory
This is the first line of the linreg.R
#!/usr/bin/env Rscript
I have tried setting path to PATH=$PATH:C:\\ProgramFiles\\R\\R-3.0.1\\bin
but of no use. It has changed the PATH but still the script is not running.
Any help will be highly appreciated.
it might be worth your time to add it to your .bashrc file:
echo 'PATH=$PATH:/cygdrive/c/Program\ Files/R/R-3.2.3/bin' >> .bashrc
I figured out the mistake that I was making again and again.
cygpath is used to find out the actual representation of the path of the the directory in UNIX environment
Example:
$ cygpath 'C:\Program Files\R\R-3.0.1\bin'
/cygdrive/c/Program Files/R/R-3.0.1/bin
So we need to make sure that PATH variable has Program Files and not ProgramFiles.
Since UNIX does not understand special characters we need to backscape the space between Program Files
$ PATH=$PATH:/cygdrive/c/Program\ Files/R/R-3.0.1/bin
It started recognizing R files after that.

Resources