Every single command ends with 'zsh: command not found: amplify_env' - terminal

I have some kind of a problem here. Every time I type down a command in my terminal, it ends up with "zsh: command not found: amplify_env". Whatever the command is.
I can type something like "test" in my terminal, it will end up with that exact same message.
It looks like my terminal is trying to execute an amplify_env command every time i do something in my terminal. Has anyone an idea from where this can come?
Thank you!

In zsh rc add this function :
# .zshrc
amplify_env () {
PROJECT_DIR=$(git rev-parse --show-toplevel 2>/dev/null)
ENV=$PROJECT_DIR/amplify/.config/local-env-info.json
if [ -f "$ENV" ]; then
env_info=$(cat $ENV | jq -r ".envName")
echo "(🚀 $env_info)"
fi
}
https://dev.to/siegerts/displaying-the-active-amplify-environment-alongside-the-current-git-branch-4hmg

Related

Rofi custom script on combi mode

I made a simple custom script to quick open a project on vscode thanks to ghq:
selection=$(ghq list | rofi -dmenu -p "Project" -no-custom)
[[ ! -z "${selection}" ]] && code ${HOME}/p/${selection}
exit 0
I bound it on i3 config:
bindsym $mod+d exec --no-startup-id rofi -show combi
bindsym $mod+p exec --no-startup-id ~/.config/rofi/project.sh
It works like a charm. Howeverm I want to make it part of the combi mode in order to have only one shortcut.
How can I achieve this?
If this is not possible with rofi, does a similar tool permit that?
It is possible to do by adding a script to your rofi config in ~/.config/rofi/config.rasi:
configuration {
combi-modi: "window,drun,ssh,Project:~/.config/rofi/project.sh";
modi: "combi";
}
Then you need to modify your script, so it will return a list of projects when no arguments are given and open a project when the script is launched with a project name as an argument:
#!/usr/bin/env sh
# List projects if no arguments given
if [ -z "$1" ]; then
ghq list
else
code "$HOME/p/$1"
fi
Now, rofi -show combi should open with a default combi mode+project launcher.
By the way, I am not sure if the exit 0 at the end of the script is needed. It makes the script always return successful status, no matter the exit status of used commands. See the answer here.
In my case, I was trying a simple file finder script for rofi
At first my attempt was:
fd -H . ~ | rofi -dmenu | xargs xdg-open > /dev/null 2>&1 &
Calling the script from the command line works like a charm, but when I tried this inside rofi, didn't work. Of course I can't call rofi indide rofi
Following the idea here, given by Cezary,I got to this:
#!/bin/bash
if [[ -z "$1" ]]; then
fd -H . ~
else
xdg-open "$1" > /dev/null 2>&1 &
fi
Turns out, de > /dev/null 2>&1 & was important, so it wouldn't freeze the whole thing. Maybe only the "&" in the end is important, so the process goes to the background? The rest, as I understand, just throws away any output or errors.
In my "config.rasi" from rofi, I have the line
modi: "drun,Finder:/path/to/script";
since I want it as a second mode, instead of a combi. Of course I made it executable with chmod +x.

Customize "command not found" message in Bash

Is there someway to alter the Bash system error message template so that you can print something in addition to the original message? For example:
Macbook Air:~/Public]$ lfe
-bash: lfe: WTF command not found
or
Macbook Air:~/Public]$ lfe
-bash: lfe: #!&**! command not found
Since Bash 4.0, if the search for a command is unsuccessful, the shell searches for a function called command_not_found_handle. If it doesn't exist, Bash prints a message like this and exits with status 127:
$ foo
-bash: foo: command not found
$ echo $?
127
If it does exist, it is called with the command and its arguments as arguments, so if you have something like
command_not_found_handle () {
echo "It's my handle!"
echo "Arguments: $#"
}
in your .bashrc, Bash will react like this:
$ foo bar
It's my handle!
Arguments: foo bar
Most systems have something much more sophisticated in place, though. My Ubuntu, for example, has this in /etc/bash.bashrc:
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
fi
and this is sourced from /etc/profile. /usr/lib/command-not-found is a Python script that uses some more Python (CommandNotFound) to basically look up packages that are named like the unknown command, or sound similar:
$ sl
The program 'sl' is currently not installed. You can install it by typing:
sudo apt install sl
$ sedd
No command 'sedd' found, did you mean:
Command 'sed' from package 'sed' (main)
Command 'seedd' from package 'bit-babbler' (universe)
Command 'send' from package 'nmh' (universe)
Command 'send' from package 'mailutils-mh' (universe)
sedd: command not found
So if you want simple customization, you can provide your own command_not_found_handle, and if you want to customize the existing system, you can modify the Python scripts.
But, as mentioned, this requires Bash 4.0 or higher.
Maybe something like:
curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
then add the following to .bashrc too
preexec() { type "$1" >/dev/null 2>&1 || echo -n 'WTF??? '; }
reload your shell, then try enter some nonexistent command, like bububu
$ bububu
will print
WTF??? -bash: bububu: command not found
Important: read https://github.com/rcaloras/bash-preexec

How to execute a script only if it is present, in bash?

I wonder if there is a simpler way to execute a script in bash only if this script exists. What I want is equivalent to:
if [ -x $name ]
then
$name
fi
or
[ -x $name ] && $name
What I am looking for is something like
exec_if_exist $name
which eliminates repetition of the script name.
Is there a way to simplify this in bash?
I do not want a function or "speculative" execution, which would give the command not found error.
Best
You can simplify it somewhat with the type command. With your tests, it requires a full or relative path to the file. With type it will search the PATH.
type $name && $name
it is also nice because it puts success on STDOUT and failure on STDERR, giving you complete control over the output
# mute success
type >/dev/null
# mute fail
type 2>/dev/null
# mute both
type &>/dev/null
Why not
exec_if_exist() {
test -x $1 && $1
}
And, the path may need to be considered when invoking $1.
type did not seem to always work on OS X. What worked was
which -s command && command

Bash script not working on a new dedicated server

Recently I have migrated to the new dedicated server which is running on the same operating system - FreeBSD 8.2. I got a root account access and all permissions have been set properly.
My problem is that, the bash script I was running on the old server doesn't works on the new machine, the only error appearing while running the script is:
# sh script.sh
script.sh: 3: Syntax error: word unexpected (expecting ")")
Here is the code itself:
#!/usr/local/bin/bash
PORTS=(7777:GAME 11000:AUTH 12000:DB)
MESSG=""
for i in ${PORTS[#]} ; do
PORT=${i%%:*}
DESC=${i##*:}
CHECK=`sockstat -4 -l | grep :$PORT | awk '{print $3}' | head -1`
if [ "$CHECK" -gt 1 ]; then
echo $DESC[$PORT] "is up ..." $CHECK
else
MESSG=$MESSG"$DESC[$PORT] wylaczony...\n"
if [ "$DESC" == "AUTH" ]; then
MESSG=$MESSG"AUTH is down...\n"
fi
if [ "$DESC" == "GAME" ]; then
MESSG=$MESSG"GAME is down...\n"
fi
if [ "$DESC" == "DB" ]; then
MESSG=$MESSG"DB is down...\n"
fi
fi
done
if [ -n "$MESSG" ]; then
echo -e "Some problems ocurred:\n\n"$MESSG | mail -s "Problems" yet#another.com
fi
I don't really code in bash, so I don't know why this happend...
Bourne shell (sh) doesn't support arrays, that's why you're running into this error when you use
sh script.sh
Use bash instead
bash script.sh
Note: I suspect that sh script.sh worked on the old server because sh is linked to bash there.
also you shouldn't need to run it through sh (that's what the
#!
on the first line is for - the OS will run the remainder of the line as a command and pass the contents of the file for it to interpret). Just make the script executable:
chmod +x script.sh
and then you can just run it directly without the sh in front of the name.
It's possible that the default shell is not bash and so by running it through sh you're interpreting it with a different shell which is then giving the error
The code looks good. It is likely that your new dedicated server is running older version of Bash than your last server. Or maybe /usr/local/bin/bash is pointing towards older version.
Run
$ which bash
if the output is other than /usr/local/bin/bash then change the first shebang line to the newer path, if it still does not work
Try replacing third line:
PORTS=(7777:GAME 11000:AUTH 12000:DB)
with
PORTS=('7777:GAME' '11000:AUTH' '12000:DB')
and rerun the script.
If it still does not work then post the BASH version here by running
$ bash --version
try with facing and trailing spaces
PORTS=( 7777:GAME 11000:AUTH 12000:DB )

Bash syntax error: unexpected end of file

Forgive me for this is a very simple script in Bash. Here's the code:
#!/bin/bash
# june 2011
if [ $# -lt 3 -o $# -gt 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
after running sh file.sh:
syntax error: unexpected end of file
I think file.sh is with CRLF line terminators.
run
dos2unix file.sh
then the problem will be fixed.
You can install dos2unix in ubuntu with this:
sudo apt-get install dos2unix
Another thing to check (just occured to me):
terminate bodies of single-line functions with semicolon
I.e. this innocent-looking snippet will cause the same error:
die () { test -n "$#" && echo "$#"; exit 1 }
To make the dumb parser happy:
die () { test -n "$#" && echo "$#"; exit 1; }
i also just got this error message by using the wrong syntax in an if clause
else if (syntax error: unexpected end of file)
elif (correct syntax)
i debugged it by commenting bits out until it worked
an un-closed if => fi clause will raise this as well
tip: use trap to debug, if your script is huge...
e.g.
set -x
trap read debug
I got this answer from this similar problem on StackOverflow
Open the file in Vim and try
:set fileformat=unix
Convert eh line endings to unix endings and see if that solves the
issue. If editing in Vim, enter the command :set fileformat=unix and
save the file. Several other editors have the ability to convert line
endings, such as Notepad++ or Atom
Thanks #lemongrassnginger
This was happening for me when I was trying to call a function using parens, e.g.
run() {
echo hello
}
run()
should be:
run() {
echo hello
}
run
I had the problem when I wrote "if - fi" statement in one line:
if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bash fi
Write multiline solved my problem:
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
So I found this post and the answers did not help me but i was able to figure out why it gave me the error. I had a
cat > temp.txt < EOF
some content
EOF
The issue was that i copied the above code to be in a function and inadvertently tabbed the code. Need to make sure the last EOF is not tabbed.
on cygwin I needed:-
export SHELLOPTS
set -o igncr
in .bash_profile . This way I didn't need to run unix2dos
FOR WINDOWS:
In my case, I was working on Windows OS and I got the same error while running autoconf.
I simply open configure.ac file with my NOTEPAD++ IDE.
Then I converted the File with EOL conversion into Windows (CR LF) as follows:
EDIT -> EOL CONVERSION -> WINDOWS (CR LF)
Missing a closing brace on a function definition will cause this error as I just discovered.
function whoIsAnIidiot() {
echo "you are for forgetting the closing brace just below this line !"
Which of course should be like this...
function whoIsAnIidiot() {
echo "not you for sure"
}
I was able to cut and paste your code into a file and it ran correctly. If you
execute it like this it should work:
Your "file.sh":
#!/bin/bash
# june 2011
if [ $# -lt 3 -o $# -gt 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
The command:
$ ./file.sh arg1 arg2 arg3
Note that "file.sh" must be executable:
$ chmod +x file.sh
You may be getting that error b/c of how you're doing input (w/ a pipe, carrot,
etc.). You could also try splitting the condition into two:
if [ $# -lt 3 ] || [ $# -gt 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
Or, since you're using bash, you could use built-in syntax:
if [[ $# -lt 3 || $# -gt 3 ]]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
And, finally, you could of course just check if 3 arguments were given (clean,
maintains POSIX shell compatibility):
if [ $# -ne 3 ]; then
echo "Error... Usage: $0 host database username"
exit 0
fi
In my case, there is a redundant \ in the like following:
function foo() {
python tools/run_net.py \
--cfg configs/Kinetics/X3D_8x8_R50.yaml \
NUM_GPUS 1 \
TRAIN.BATCH_SIZE 8 \
SOLVER.BASE_LR 0.0125 \
DATA.PATH_TO_DATA_DIR ./afs/kinetics400 \
DATA.PATH_PREFIX ./afs/kinetics400 \ # Error
}
There is NOT a \ at the end of DATA.PATH_PREFIX ./afs/kinetics400
I just cut-and-pasted your example into a file; it ran fine under bash. I don't see any problems with it.
For good measure you may want to ensure it ends with a newline, though bash shouldn't care. (It runs for me both with and without the final newline.)
You'll sometimes see strange errors if you've accidentally embedded a control character in the file. Since it's a short script, try creating a new script by pasting it from your question here on StackOverflow, or by simply re-typing it.
What version of bash are you using? (bash --version)
Good luck!
Make sure the name of the directory in which the .sh file is present does not have a space character. e.g: Say if it is in a folder called 'New Folder', you're bound to come across the error that you've cited. Instead just name it as 'New_Folder'. I hope this helps.
Apparently, some versions of the shell can also emit this message when the final line of your script lacks a newline.
In Ubuntu:
$ gedit ~/.profile
Then, File -> Save as and set end line to Unix/Linux
I know I am too late to the party. Hope this may help someone.
Check your .bashrc file. Perhaps rename or move it.
Discussion here: Unable to source a simple bash script
For people using MacOS:
If you received a file with Windows format and wanted to run on MacOS and seeing this error, run these commands.
brew install dos2unix
sh <file.sh>
If the the script itself is valid and there are no syntax errors, then some possible causes could be:
Invalid end-of-lines (for example, \r\n instead of \n)
Presence of the byte order mark (BOM) at the beginning of the file
Both can be fixed using vim or vi.
To fix line endings open the file in vim and from the command mode type:
:set ff=unix
To remove the BOM use:
:set nobomb
For those who don't have dos2unix installed (and don't want to install it):
Remove trailing \r character that causes this error:
sed -i 's/\r$//' filename
Details from this StackOverflow answer. This was really helpful.
https://stackoverflow.com/a/32912867/7286223

Resources