linux cp command different behavior between cmd line and shell script. - linux

When building gcc-4.8.1, I met a cp command as following:
#!/bin/sh
set -x
fname="cp.sh"
cp -v $fname.{,.bk}
It will occurs error when execute this script:
<29>pli[1050]#~/workspace/shell*0 > sh cp.sh
+ fname=cp.sh
+ cp -v cp.sh{,.bk}
cp: missing destination file operand after âcp.sh{,.bk}â
Try 'cp --help' for more information.
<30>pli[1051]#~/workspace/shell*0 >
But when type it on cmd line directly, it works well as expected. It is very in comprehensive to me.\n
<28>pli[1049]#~/workspace/shell*0 > cp -v cp.sh{,.bk}
cp.sh -> cp.sh.bk
<29>pli[1050]#~/workspace/shell*0 > ^C

You need to build gcc with "bash". On Ubuntu derived distributions "/bin/sh" in not linked to "/bin/bash" (but to "/bin/dash"); this is documented in the Ubuntu wiki here. I would suggest using a "ppa" if possible.

Related

Error while trying to install Kops on Ubuntu 20 EC2 Instance

I went through the steps listed here: https://kubernetes.io/docs/setup/production-environment/tools/kops/
After moving the kops file to /usr/local/bin/ and renaming to kops, I tried to confirm if it was in fact installed and executable by trying 'kops --help' and 'kops --version'/'kops version' and neither command worked. Any idea what the issue might be?
Edit: Here's what I did step by step
curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-darwin-amd64
sudo chmod +x kops-darwin-amd64
sudo mv kops-darwin-amd64 /usr/local/bin/kops
It's a t2.micro Ubuntu 20.04 EC2 Instance.
Tried to confirm if kops was properly installed and executable by entering 'kops --help' and 'kops --version' and also 'kops version' but they all return this error:
-bash: /usr/local/bin/kops: cannot execute binary file: Exec format error
I think its because you are using kops-darwin-amd64. This is for mac. I think you should be using kops-linux-amd64 instead for linux.

Shell Script won't run from NodeJS because it has "set -o pipefail" [duplicate]

The below mentioned line of code used to work for me all the time on a Ubuntu 16.04 distribution, but suddenly option-name pipefail is an illegal option:
set -eu -o pipefail
returns:
set: Illegal option -o pipefail
Why does this happen? I run the command on a completely new installed system and as part of a shell script. The code is placed right at the beginning:
myscript.sh:
1 #!/bin/bash
2 set -eu -o pipefail
3 ...
The script is run as sudo:
sudo sh ./myscript.sh
You are running bin/sh, on Ubuntu it is a symbolic link pointing to /bin/dash, but pipefail is a bashism.
Make the script executable:
chmod +x myscript.sh
and then run the script as follows:
sudo ./myscript.sh
I had the same error when running script from zsh and the script began with incorrect shebang.
WRONG, missing ! after #:
#/bin/bash
rest-of-the-script
Correct:
#!/bin/bash
rest-of-the-script

bash: cd: No such file or directory

I'm writing a bash function to jump into my last editted folder.
In my example, the last edited folder is titled 'daniel'.
The bash function looks fine.
>>:~$ echo $(ls -d -1dt -- */ | head -n 1)
daniel/
And I can manually cd into the directory.
>>:~$ cd daniel
>>:~/daniel$
But I can't use the bash function to cd into the directory.
>>:~$ cd $(ls -d -1dt -- */ | head -n 1)
bash: cd: daniel/: No such file or directory
Turns out someone added alias ls=ls --color to the bashrc of this server. My function works once the alias was removed. – Daniel Tan
This error is usually thrown when you enter a path that does not exist. See -bash: cd: Desktop: No such file or directory.
But the $(ls -d -1dt -- */ | head -n 1) is not wrong in the output. Thus the reason must be the different usage of sh and bash in that moment.
In my case, I had a docker container with that error when I accessed the folder with bash. The container was broken since I had force-closed it after docker-compose up which did not work. After that, on the existing containers, I could only use sh, not bash. I found this because of OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: "bash": executable file not found in $PATH": unknown. I guess that bash is loaded later than sh, and that at an early error at the start of the container, only sh gets loaded.
That would fit since you are in the sh, which can be seen from >>. Using sh, everything will work as expected. But the expression gets solved by bash. Which is probably not loaded for whatever reason.
In docker, using docker-compose, I also had a similar error saying sh: 1: cd: can't cd to /root/MYPROJECT. That could be solved by mounting the needed volumes in the services using
services:
host:
volumes:
- ~/MYPROJECT:/MYPROJECT # ~/path/on/host:/path/on/container
See Mount a volume in docker-compose. How is it done? and How to mount a host directory with docker-compose? or the official docs.

Bash: sourcing file as user from script

I am creating a script meant to be run as superuser that reads a file and runs a number of scripts on behalf of all users. The important bit is this:
sudo -u $user -H source /home/$user/list_of_commands
However, whether I encose the command with quotesor not, this fails with:
sudo: source /home/user/list_of_commands: command not found
I have even tried with the . bash builtin:
sudo: . /home/user/list_of_commands: command not found
Of course running source outside a sudo environment works. I thought there might be a PATH problem, and I tried to bypass it by providing the full path to source. However, I cannot find the executable: which source returns which: no source in (/usr/local/sbin:usr/local/bin:usr/bin). So I'm stuck.
How do I make a script source a file as a user?
source is a builtin not a command, use it with bash -c:
sudo -u $user -H bash -c "source /home/$user/list_of_commands"

some error with the 'ln' scripts

anybody is familiar with the etcd project? Or we'd better forget the project when talk about this issue. The issue is
$ build
ln: `gopath/src/github.com/coreos/etcd': cannot overwrite directory
when exec the build shell
and the content is:
#!/bin/sh -e
if [ ! -h gopath/src/github.com/coreos/etcd ]; then
mkdir -p gopath/src/github.com/coreos/
ln -s ../../../.. gopath/src/github.com/coreos/etcd
fi
export GOBIN=${PWD}/bin
export GOPATH=${PWD}/gopath
export GOFMTPATH="./bench ./config ./discovery ./etcd ./error ./http ./log main.go ./metrics ./mod ./server ./store ./tests"
# Don't surprise user by formatting their codes by stealth
if [ "--fmt" = "$1" ]; then
gofmt -s -w -l $GOFMTPATH
fi
go install github.com/coreos/etcd
go install github.com/coreos/etcd/bench
Some addition:
My system is windows 7
I run the shell on git bash.
issue reproduce:
step1: open the git bash
step2: git clone git#github.com:coreos/etcd.git
step3: cd etcd
step4: build
As mentioned in "Git Bash Shell fails to create symbolic links" (since you are using the script in a git bash on Windows 7)
the ln that shipped with msysGit simply tries to copy its arguments, rather than fiddle with links. This is because links only work (sort of) on NTFS filesystems, and the MSYS team didn't want to reimplement ln.
A workaround is to run mklink from Bash.
This also allows you to create either a Symlink or a Junction.
So 'ln' wouldn't work as expected by default, in the old shell that ships with Git for Windows.
Here's solution. Tbh it is a workaround, but since you're on Windows, I don't see another way.
Start a command line, and enter there to the directory with the script. There should be a path gopath/src/github.com/coreos/ (if no such a path, you must create it). Next issue a command
mklink /D "gopath/src/github.com/coreos/etcd" "../../../../"
Next you should edit the build script to delete a lines with creation symlink and a directory. E.g.
#!/bin/sh -e
export GOBIN=${PWD}/bin
export GOPATH=${PWD}/gopath
export GOFMTPATH="./bench ./config ./discovery ./etcd ./error ./http ./log main.go ./metrics ./mod ./server ./store ./tests"
# Don't surprise user by formatting their codes by stealth
if [ "--fmt" = "$1" ]; then
gofmt -s -w -l $GOFMTPATH
fi
go install github.com/coreos/etcd
go install github.com/coreos/etcd/bench
Note, that I am just removed 4 lines of code. Next you run the script, and this should work.
You shouldn't be using git clone and the build sh script. Use the go get command. For example, on Windows 7,
Microsoft Windows [Version 6.1.7601]
C:\>set gopath
GOPATH=C:\gopath
C:\>go version
go version go1.3 windows/amd64
C:\>go get -v -u github.com/coreos/etcd
github.com/coreos/etcd (download)
github.com/coreos/etcd/third_party/bitbucket.org/kardianos/osext
github.com/coreos/etcd/pkg/strings
github.com/coreos/etcd/error
github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd
github.com/coreos/etcd/http
github.com/coreos/etcd/third_party/github.com/coreos/go-log/log
github.com/coreos/etcd/third_party/github.com/rcrowley/go-metrics
github.com/coreos/etcd/mod/dashboard/resources
github.com/coreos/etcd/log
github.com/coreos/etcd/third_party/github.com/gorilla/context
github.com/coreos/etcd/third_party/github.com/gorilla/mux
github.com/coreos/etcd/mod/dashboard
github.com/coreos/etcd/discovery
github.com/coreos/etcd/pkg/btrfs
github.com/coreos/etcd/pkg/http
github.com/coreos/etcd/third_party/code.google.com/p/gogoprotobuf/proto
github.com/coreos/etcd/mod/leader/v2
github.com/coreos/etcd/mod/lock/v2
github.com/coreos/etcd/metrics
github.com/coreos/etcd/third_party/github.com/mreiferson/go-httpclient
github.com/coreos/etcd/mod
github.com/coreos/etcd/third_party/github.com/BurntSushi/toml
github.com/coreos/etcd/third_party/github.com/goraft/raft/protobuf
github.com/coreos/etcd/third_party/github.com/goraft/raft
github.com/coreos/etcd/store
github.com/coreos/etcd/server/v1
github.com/coreos/etcd/server/v2
github.com/coreos/etcd/store/v2
github.com/coreos/etcd/server
github.com/coreos/etcd/config
github.com/coreos/etcd/etcd
github.com/coreos/etcd
C:\>

Resources