String concatenation does not work in Bash cygwin - string

Tell me this, why this code does not work in Cygwin ?
I've tried use \. and different symbol combinations between variable.
Scopes tried too ( round, figure ).
Dunno what up.
My code is here and it WORKS !!!: http://ideone.com/0tLmzu
NOTE: I just trying to concatenate two strings in one, but Cygwin can't do that.
Example below:
echo $a$b prints only $b, not $a and $b

I believe I know what this is. The file is a dos file and not unix. If you use vim you can:
vim file
:set ff=unix
:x
Or if you do not want to use vim, this from the command line:
dos2unix file

Related

vim interpret argument with colon(s) as filename:line:column

Is it possible to configure VIM in a such way that if I type
vim filename:123:89
it opens file filename goes to line 123 and column 89?
If not through VIM maybe with a hack for the shell?
You can install the file-line plugin to open a file to the line and column specified after the filename. (github mirror)
From the Readme on github
When you open a file:line, for instance when coping and pasting from
an error from your compiler vim tries to open a file with a colon in
its name.
Examples:
vim index.html:20
vim app/models/user.rb:1337
With this little script in your plugins folder if the stuff after the colon is a number and a file exists with the name especified before the colon vim will open this file and take you to the line you wished in the first place.
I'm not sure how to skip to the column, but I've wanted the same feature for ages, so I just hacked up the "jump to line" functionality. In your .bashrc, set
VIM=$(which vim)
function vim {
local args
IFS=':' read -a args <<< "$1"
"$VIM" "${args[0]}" +0"${args[1]}"
}
This splits the argument to Vim by :, then constructs a command line of the form
vim <filename> +0<line>
The +0 is a hack to make sure the default line number is zero.
(If you're not using Bash, you can adapt this into a script and put it in your path, or translate it to your favorite shell language. To edit filename:with:colons, use $VIM.)
I've been using the file-line plugin, but it has a few open issues, and breaks some other vim plugins. So I went fishing for a better solution. Here it is:
function vim() {
local first="$1"
case $first in
*:*)
shift
command vim ${first%%:*} +0${first##*:} $#
;;
*)
command vim $#
;;
esac
}
Limitations:
bash only
Only parses first argument, whereas vim +X parses the first file argument. A more complex version could easily be made with proper command line parsing.
Advantages:
doesn't break other vim plugins
you could easily use $EDITOR and use this with emacs for instance.
compared to Fred's answer it doesn't use IFS/read to parse the argument but uses bash parameter expansion.
also sends in the remaining argument, which might occasionally be necessary.

Command not found error while shell scripting

I'm trying to execute the program as followed.
./chExt1.sh cpp test.CPP
This should rename test.CPP to test.cpp but I don't even think this script is executing at all.
I am consistently getting this "command not found error".
The script is below :
#!/bin/sh
newExtension=$1;
oldFile=$2;
firstPart=`echo $oldFile | sed 's/\(.*\)\..*/\1/'`
newName="$firstPart.$newExtension";
#echo $oldFile
#echo $newName
mv "$oldFile" "$newName"
#echo "$oldFile"
#echo "$firstPart"
#echo "$newName"
I finally fixed the issue. Something went horribly wrong when I FTP'd the text file which contained the script and then just transferred it inside of a .sh in linux. I wrote in from scratch in emacs and that cleared everything up.
Based on your comment, do this in vi to remove the extra control characters. I have had this problem before when editing files in gedit or when editing in Windows and then using on a Unix/Linux machine.
To remove the ^M characters at the end of all lines in vi, use:
:%s/^V^M//g
The ^v is a CtrlV character and ^m is a CtrlM. When you type this, it will look like this:
:%s/^M//g
In UNIX, you can escape a control character by preceeding it with a CtrlV. The :%s is a basic search and replace command in vi. It tells vi to replace the regular expression between the first and second slashes (^M) with the text between the second and third slashes (nothing in this case). The g at the end directs vi to search and replace globally (all occurrences).
Source

Why control characters appended after bash command?

I used the bash commands to append several lines to multiple configuration files:
> for filename in *.ovpn; do
> printf 'configurationscript-security 2\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf' >> $filename;
> done
However the control character "^M" appeared at end of each line in the configuration file:
I opened the files in vim, the files before bash commands looked like as folows:
I am curious why "^M" appears at end of each line? Thanks.
It is Windows' carriage return, use dos2unix to convert file. Vim recognize the file format and displays it correctly.
The ^M can also be removed via a regular expression in vim, if dos2unix isn't available.
:%s/^M//g, which can be entered as: Esc:%s/ctrl+Vctrl+M//g
Not sure why this has occurred for you with just a simple printf command on a linux system, maybe have a look that you're picking up the correct version of printf. I've given this a go on a linux system, and the local printf keeps the correct line-endings, as you would expect.

How can I run a !<cmd> from a vim script when the path has a space in it on Windows?

I'm attempting to use the :!<cmd> format in vim to execute an external command and put the results in the buffer. If I type :!, path completion is possible and I can complete the path right up to the command I want to execute. This automatically escapes spaces like so:
:!c:\Program\ Files\ (x86)\Microsoft\ Visual\ Studio\ 9.0\Common7\IDE\TF.exe
When I hit enter, I get:
'c:\Program\' is not recognized as an internal or external command
Which I suspect means that vim has not escaped the spaces properly when passing the command to cmd.exe. I've tried all sorts of escaping combinations to make this work but to no avail. The only way I've found to do this is to work out what the DOS8.3 filename is and use that instead of the long path name. However, I don't like this approach since it's going to make my script less portable. Does anyone know if this can be done, or is it a bug in vim?
If you have quoted arguments, not just the exe path, then you may need to do some fancy quoting, like below. The main problem is not the exe path itself, but the arguments. I found this webpage helpful for similar problems myself:
http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
Not sure offhand and don't have time to check, but if you have a quoted argument then sample below may be closer to what you need:
silent! exe 'r!"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\TF.exe" history /followbranches \^"#\^"'
Also, I wonder whether the quotes around the path may need special treatment since they are around only a portion of the full command. In any case, the quotes \^" work for main quotes in command line and ^" for quotes embedded in other quotes. I have in the past found it useful to experiment with the command at a windows prompt, remembering to test it with the way Vim prepares it, which is with your command prepended by c:\windows\sys32\cmd.exe .
On second thought, I think when I was working with similar problem I never did get to point of solving command with both quoted arguments and quoted exe-path-with-spaces in same command. I expect there's way to do it, but I instead just created a soft link to the exe in path with no spaces. E.g.:
mklink c:\users\myname\myexe c:\program files(x86)\myapp\myexe.exe
After having done that there's no need to quote the exe command itself and quoting the argument with \^" worked fine. I am of course curious about how to quote an exe-with-spaces that also has quoted arguments.
EDIT: I think I found way around my problem with quoting, don't have VS to test with your exact command but here's what I think may work from command line:
cmd /k ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\TF.exe" history /followbranches ^"#^""
If that works for you from command line then I think only issue is getting Vim to include the /k switch. (Also, there could be issue with Windows command line "throwing away" the /followbranch switch, because of the forward slash, but maybe not.)
EDIT2: I think the trick for doing it from Vim is just to include the 'cmd /k' as part of the command you're running. You end up with several levels of shells opening, but I don't think that's a problem. For an example, here's on that runs from Vim, with (1) spaces in exe path, (2) quoted argument (the (message .. ) ) and even (3) a quote within a quoted argument (\^"hi\^"). This command opens an Emacs instance and has Emacs print message "hi":
!cmd /k ""c:\program files (x86)\emacs\emacs\bin\emacs.exe" --eval ^"(message \^"hi\^")^""
And yet one more EDIT: Including your own 'cmd /k' does create problems, I think, if you're trying not just to execute the external command, but to read its output back into the Vim buffer. In that case you could redirect the output to a file in the user's home directory and the use :read to insert into the buffer. If there's some way to get Vim's own cmd to use k switch then this would be unnecessary, but if not then at least this provides good workaround.
Enclose the full pathname of the executable in double quotation marks. Do not escape spaces in the pathname.
In your example, some of the backslashes were added to escape spaces, and others are a part of the pathname. You did not provide the original pathname, but I can guess at it. If I guessed right, the command that will work is:
:!"c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\TF.exe"
This works equally well in a script. The equivalent script command is:
silent execute '!"c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\TF.exe"'
I have tested this in Vim 7.3.346 x86, installed on Windows 7 Pro SP1 x64.
%0 (batch name) %1 (1st parameter) %2 (2nd parameter)
example:
C:\CSW>MyBatchFile.bat "C:\Program files" "C:\CSW\My File.txt"
Not sure if this works with vim but it does work with bash in windows.
You just need to call by adding double quote in it.
I dont have enough idea about Vim script. but while running in command prompt if you will give the complete exe path having space then it will give error like
C:>c:\Program Files\Internet Explorer\iexplore.exe
'c:\Program' is not recognized as an internal or external command,
operable program or batch file.
But It will work if you will surrounded with double qoute.
C:>"c:\Program Files\Internet Explorer\iexplore.exe"
C:>

Bash script prints "Command Not Found" on empty lines

Every time I run a script using bash scriptname.sh from the command line in Debian, I get Command Not found and then the result of the script.
The script works but there is always a Command Not Found statement printed on screen for each empty line. Each blank line is resulting in a command not found.
I am running the script from the /var folder.
Here is the script:
#!/bin/bash
echo Hello World
I run it by typing the following:
bash testscript.sh
Why would this occur?
Make sure your first line is:
#!/bin/bash
Enter your path to bash if it is not /bin/bash
Try running:
dos2unix script.sh
That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF).
More details about the dos2unix command (man page)
Another way to tell if your file is in dos/Win format:
cat scriptname.sh | sed 's/\r/<CR>/'
The output will look something like this:
#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>
This will output the entire file text with <CR> displayed for each \r character in the file.
You can use bash -x scriptname.sh to trace it.
I also ran into a similar issue. The issue seems to be permissions. If you do an ls -l, you may be able to identify that your file may NOT have the execute bit turned on. This will NOT allow the script to execute. :)
As #artooro added in comment:
To fix that issue run chmod +x testscript.sh
This might be trivial and not related to the OP's question, but I often made this mistaken at the beginning when I was learning scripting
VAR_NAME = $(hostname)
echo "the hostname is ${VAR_NAME}"
This will produce 'command not found' response. The correct way is to eliminate the spaces
VAR_NAME=$(hostname)
On Bash for Windows I've tried incorrectly to run
run_me.sh
without ./ at the beginning and got the same error.
For people with Windows background the correct form looks redundant:
./run_me.sh
If the script does its job (relatively) well, then it's running okay. Your problem is probably a single line in the file referencing a program that's either not on the path, not installed, misspelled, or something similar.
One way is to place a set -x at the top of your script or run it with bash -x instead of just bash - this will output the lines before executing them and you usually just need to look at the command output immediately before the error to see what's causing the problem
If, as you say, it's the blank lines causing the problems, you might want to check what's actaully in them. Run:
od -xcb testscript.sh
and make sure there's no "invisible" funny characters like the CTRL-M (carriage return) you may get by using a Windows-type editor.
use dos2unix on your script file.
for executing that you must provide full path of that
for example
/home/Manuel/mywrittenscript
Try chmod u+x testscript.sh
I know it from here:
http://www.linuxquestions.org/questions/red-hat-31/running-shell-script-command-not-found-202062/
If you have Notepad++ and you get this .sh Error Message: "command not found"
or this autoconf Error Message "line 615:
../../autoconf/bin/autom4te: No such file or directory".
On your Notepad++, Go to Edit -> EOL Conversion then check Macinthos(CR).
This will edit your files. I also encourage to check all files with this command,
because soon such an error will occur.
Had the same problem. Unfortunately
dos2unix winfile.sh
bash: dos2unix: command not found
so I did this to convert.
awk '{ sub("\r$", ""); print }' winfile.sh > unixfile.sh
and then
bash unixfile.sh
Problems with running scripts may also be connected to bad formatting of multi-line commands, for example if you have a whitespace character after line-breaking "\". E.g. this:
./run_me.sh \
--with-some parameter
(please note the extra space after "\") will cause problems, but when you remove that space, it will run perfectly fine.
I was also having some of the Cannot execute command. Everything looked correct, but in fact I was having a non-breakable space right before my command which was ofcourse impossible to spot with the naked eye:
if [[ "true" ]]; then
highlight --syntax js "var i = 0;"
fi
Which, in Vim, looked like:
if [[ "true" ]]; then
highlight --syntax js "var i = 0;"
fi
Only after running the Bash script checker shellcheck did I find the problem.
I ran into this today, absentmindedly copying the dollar command prompt $ (ahead of a command string) into the script.
Make sure you havenĀ“t override the 'PATH' variable by mistake like this:
#!/bin/bash
PATH="/home/user/Pictures/"; # do NOT do this
This was my mistake.
Add the current directory ( . ) to PATH to be able to execute a script, just by typing in its name, that resides in the current directory:
PATH=.:$PATH
You may want to update you .bashrc and .bash_profile files with aliases to recognize the command you are entering.
.bashrc and .bash_profile files are hidden files probably located on your C: drive where you save your program files.

Resources