Unable to prepare context: path ".\r" not found - linux

I have created build_docker.sh file in windows machine which has below contents.
#!/usr/bin/env bash
#This script build docker image
docker build -t hello-world .
Where I try to run above file using command sudo sh build_docker.sh
It fails with an error, Unable to prepare context: path ".\r" not found
I am using 4.2.46(2)-release Bash version
What I am missing here?

There is an utility to convert file to unix EOL, it helped me.
dos2unix build_docker.sh

… using :set fileformat=unix and sudo bash build_docker.sh solved my problem
– Mayur

changing file name from filename.sh to .bash solved my issue.

This will also work.
1. Open all files using vim *
2. Press "qq" to start recording
3. :set ff=unix
4. :wn
5. Press again "q" to stop recording
6. :wq!

Related

Node script executable not working on Mac : env: node\r: No such file or directory

I have created my node script executable to execute some tasks grunt. On Windows, my node script works fine. But on Mac OS X (Yosemite), it's not working.
My node script has been published on Windows.
My node script is installed via npm command :
npm install -g task-app
My node script have this first line :
#! /usr/bin/env node
I tried many some solutions to solve my problem but I'm still stuck.
Here's these solutions that I used :
uninstall and reinstall Node.js
execute this command to
create a link for node : sudo ln -s /usr/bin/nodejs
/usr/local/bin/node
set my path with this command : export PATH=$PATH:/usr/local/bin/node
Do you have other solutions to propose ?
EDIT :
the beginning of my script :
#! /usr/bin/env node
var grunt = require('grunt');
//Get parameters from command line
var args = process.argv.splice(2);
[...]
After all, I found the solution to my problem.
As my node script file has been created on Windows, the file is DOS format (line endings in DOS format I think). So, I used a module which allow to converting a file to a unix format :
brew install dos2unix
sudo dos2unix /usr/local/lib/node_modules/task-app/src/task-app.js
You could also use vim:
vim script
:se ff=unix
:wq
That will confirm DOS-style newlines to Unix-style newlines.
There is a problem with newlines in your script. Make sure that #!/usr/bin/env node is followed by \n (unix style) instead of \r\n (windows/dos style).
To fix that, use the tr command to remove \r's from your file:
cat your_script.js | tr -d '\r' > fixed_script.js
As PauloDev says above, this is a Mac/Windows line ending issue. To elaborate, if you are using nvm you'll need to locate your script first (in my case I'm using express-mvc-generator):
# install dos2unix
brew install dos2unix
# output the full path of your node version
which node
>> /Users/<username>/.nvm/versions/node/v8.0.0/bin/node
# confirm the file path
cat /Users/<username>/.nvm/versions/node/v8.0.0/lib/node_modules/express-mvc-generator/bin/express
# convert the line endings
sudo dos2unix /Users/<username>/.nvm/versions/node/v8.0.0/lib/node_modules/express-mvc-generator/bin/express
# then run your script
Reason:
This is typically due to a difference in line endings, especially the difference in LF vs. CRLF . Unix systems like Linux and macOS use LF , the line feed character, for line breaks by default. Windows, on the other hand, is special and uses CR/LF , carriage return AND line feed character, by default.
Ref: https://qvault.io/clean-code/line-breaks-vs-code-lf-vs-crlf/
Solution:
For mac users, change CRLF to LF in that file in which the error occurred.
This should no longer be a problem since npm#^5.4.0. npm will now auto-convert to the correct line endings. See https://github.com/npm/npm/issues/12371.
This is, however, still an issue in yarn: https://github.com/yarnpkg/yarn/issues/5480.
If you've come to this page because you've encountered this error when using yarn instead of npm, like I did, you might want to consider using npm instead of yarn. npm has most of yarn's best features these days, anyway (arguably).
The carriage return inserted by MS-DOS is interpreted as part of the script interpreter name, which is the correct behavior for Un*x systems by the way. Hence, the system looks for a file /usr/bin/node\r instead of /usr/bin/node. As others have pointed out, npm now "fixes" the problem by stripping off the newline character which is a somewhat dubious behavior.
Executable files with a shebang line that has a DOS line ending are corrupt and must be fixed by the author and not by users, npm, or yarn. At the time of this writing, there is little reason to still use DOS line endings, even if you develop on Windows systems. But you should at least fix the files you produce before distributing them to the general public. See https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings for how to configure git to handle line endings correctly.
The first command tells Git to never change line endings (in the future). Next, we refresh each repository by removing every file from Git's index and, finally, rewriting the Git index to pick up all the new line endings. This fixes the CRLFs that were introduced to your local file system when you cloned each repository.
Run this command:
git config core.autocrlf false
git rm --cached -r .
git reset --hard

How to make terminal recognize command "gvim" or "mvim"?

I am not sure I understand correctly from the readme file how to install vim onto my mac. After installation, I still cannot seem to open files while typing gvim HelloWorld.c into terminal. How can I permanently fix the gvim command for terminal to recognize all the time?
Mac OS X already ships with a slightly underpowered but perfectly working vim build so you only have to issue that command in your terminal:
$ vim filename
MacVim is a more featureful version that comes with a GUI and a CLI executable. Is that what you installed?
The archive you downloaded contains MacVim.app and a shell script called mvim.
If you don't plan to use Vim in or from your terminal, you don't need that mvim script at all.
If you do, the simplest solution is to put that script somewhere in your $PATH. To do so, check the value of $PATH with the following command:
$ echo $PATH
and copy the script into one of the listed directories:
$ cp /path/to/mvim /some/directory/mvim
After that's done, you can call the MacVim GUI from your terminal with:
$ mvim filename
or the MacVim CLI with:
$ mvim -v filename

Linux command to DOS

I have a file include some linux command and I want to run in on windows (DOS command).
The command is:
cat tmp/$id/index.html | sed -e 's/ID/$id/g' > a;mv a tmp/$id/index.html
What is the similar command in MS-DOS?
Thank you!
The problem is that natively there is no equivalent command to sed. You have two options from my point of view. Either create a vb script that does what you want (It will not take 1 line though - more like 10-15 I guess), or use something like GnuWin32 that gives you the option to run unix commands in windows terminal.
You could consider using powershell to do approximately the same thing. It supports cat and mv and you can get a sed like equivalent by using %{_ -replace "expression", "replace"}. Details here http://blogs.msdn.com/b/zainnab/archive/2007/07/09/grep-and-sed-with-powershell.aspx
Or consider using a linux like command prompt like bash which should be available through cygwin
I think this is impossible to do in "bare" command line (as you called DOS command), because cat and sed are separate utilities. If you want to port this script from Linux command shell to windows command line, I would advise you to download and install CygWin
DOS itself does not have support for that. You could try with a port of SED for DOS available here. If you can get Powershell, that's an option. Here's an example of using grep/sed with Powershell.
There are many options.
You can try to install cygwin or download and install Git and use Git-bash or add the bin directory to your PATH so you can run this command on your CMD prompt.
There is no such command(s) for MS-DOS.

How to specify a editor to open crontab file? "export EDITOR=vi" does not work

I'm using Red Hat Enterprise Linux 5, and I want to set the vim editor to edit the crontab file.
If I run echo $EDITOR, I get vim. But when I run crontab -e, I get different editor.
Very probable that your VISUAL environment variable is set to something else. Try:
export VISUAL=vi
To quote the man:
The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables
Most often if you run crontab -e from X, you have VISUAL set; that's what is used. Try this:
VISUAL=vi crontab -e
It just worked for me :)
If the above methods don't work (as they didn't work on my Ubuntu 13.04 installation) try:
There are a number of alternative ways:
1) Run select-editor
select-editor
2) Manually edit the file: ~/.selected_editor specifying your preferred editor. With this option you can specify editor parameters.
# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/emacs -nw"
3) You can specify on the fly on the commandline with:
env VISUAL="emacs -nw" crontab -e
You can use below command to open it in VIM editor.
export VISUAL=vim; crontab -e
Note: Please make sure VIM editor is installed on your server.
I think you might need to use the full path:
export EDITOR=/usr/bin/vim
export EDITOR=vim worked for me
It wasn't working for me. I run crontab with sudo, so I switched to root, did the above suggestions, and crontab would open in vim, but it still wouldn't from my user account. Finally I ran sudo select-editor from the user account and that did the trick.
This worked for me :
EDITOR="/usr/bin/vim"
export EDITOR
Add this to ~/.bash_profile or ~/.bashrc to enable this for current user.

How to fix path variable in bash on Mac OSX Snow Leopard

This might be a noob question, but I need help. I screwed up my terminal by trying to alter my path variable using the following command:
$ sudo nano .profile
Before I did that, if I were to type:
$ echo $PATH
I would get: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
When I opened .profile in nano it told me that the file didn't exist. I figured that made sense, since I had never edited this file before. I proceeded to enter a path to a directory I was using for a php framework and saved the file.
After I saved the file, I noticed that none of my bash commands are working. Now I can't do anything from the terminal. I can't even edit .profile in nano because it says -bash: nano: command not found
I'm clearly new to working with the terminal. I feel completely lost. Please provide some guidance on how to restore the terminal to working condition.
Use absolute paths.
$ /usr/bin/sudo /usr/bin/nano .profile
If you add something to a path, never just do
PATH=/path/to/something
instead do
PATH=$PATH:/path/to/something
By the way, you shouldn't/don't have to use sudo to edit your own file, such as .profile. Use sudo only when you need to edit the file which doesn't to belong to your account.
I had the same problem!
The way I solved was writing the follow command in the terminal:
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/usr/local/git/bin:/usr/X11/bin
Hope it can be useful for you

Resources