Linux bash line override itself [duplicate] - linux

This question already has answers here:
Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?
(3 answers)
Closed 7 years ago.
I made my own .bashrc (part of code below) and in test by pressed arrows up/down: i found that
history of commands override static text of line or leaves last command and print new over it. How to fix that?
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RESTORE=$(echo -en '\033[0m')
RKNAME=$(echo -en '\033[38;5;106m')
RKPATH=$(echo -en '\033[38;5;229m')
RKBRANCH=$(echo -en '\033[38;5;44m')
RKGIT=$(echo -en '\033[38;5;250m')
pathToFolder=$PWD;
if [[ $pathToFolder =~ .*_GitRepo.* ]]
then
PS1='${RKNAME}\u#\h${RESTORE} ${RKGIT}Git ${RKPATH}\W\ ${RKBRANCH}$(parse_git_branch) \n ${RKGIT}-- $ ${RESTORE} '
fi

Resolve my problem is:
However, I had the same line-wrapping problem you did. The fix was to insert [ and ] around the ANSI escapes so that the shell knows not to include them in the line wrapping calculation.
Thanks, #Gillies for link Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?

Related

Shell script is a directory issue [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 1 year ago.
echo $dir #prints chia-blockchain/
echo $dir | sed -e s,-blockchain/,, # prints chia
forkname = `$dir | sed -e s,-blockchain/,,`# not working
For some reason last line gives me this error :
./newfork.sh: line 15: chia-blockchain/: Is a directory
./newfork.sh: line 15: forkname: command not found
First, doing forkname = ... make the shell believe you are trying to execute a command called forkname. It should be forkname=.... You second problem is that you have $dir | sed..., you have missed out the echo, echo $dir | sed..., in that it is trying to execute whatever is in $dir.

How to echo this entire code to .bashrc without leaving out characters/strings? [duplicate]

This question already has answers here:
How do I properly quote this bash pipeline for watch?
(2 answers)
Closed 3 years ago.
How can I echo this entire piece of code to .bashrc without leaving out a single character?
# automatic logging of terminal input/output
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/$(date +"%Y-%m- %d_%H:%M:%S")_terminal.log)
When I attempt to enter the following into terminal:
echo "the above code" >> ~/.bashrc
I get the following appended to .bashrc which is nothing like "the above code", its short about 45 or so characters.
# automatic logging of terminal input/output
test script == 'script' || (script -f -q /home/user/.logs/terminal/manjaro/2019-05- 08_09:09:19_terminal.log)
As you can see, it's leaving out A LOT of the original code. I understand this has a lot to do with the number of different quotations and placement, but without altering my code much, or at least to the point where it can still function as its intended, how can I go about getting this to echo to the file properly?
Thank you for every nanosecond of your time.
Wrap your echo'd string with single quotes ' instead of double "

Bash script fails to recognise boolean logic, "command not found" [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 6 years ago.
I'm new to Bash scripts, trying to make my first backup script. When I run it I always get something like this:
./backupscript.sh: line 9: [3=1: command not found
./backupscript.sh: line 9: 3=2]: command not found
./backupscript.sh: line 15: [3=1: command not found
./backupscript.sh: line 15: 3=3]: command not found
I have tried many different syntax, like ["var"="1"]||["var"=2], double brackets, without quotes, ()-brackets single and double and I'm losing my mind. It seems like bash isn't recognising at all that it's an if-statement. What's wrong? Thanks!
#!/bin/bash
cd /
NOW=$(date +"%m_%d_%Y")
echo "Please input: 1=full 2=system 3=home"
read choice
if ["$choice"="1" || "$choice"="2"]; then
echo "--- STARTING SYSTEMBACKUP ---"
tar -cvpzf systembackup_$NOW.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/systembackup.tar.gz --exclude=/mnt --exclude=/sys --exclude=/dev --exclude=/run --exclude=/media --exclude=/home /
echo "--- SYSTEM BACKUP DONE ---"
fi
if ["$choice"="1" || "$choice"="3"]; then
echo "--- STARTING HOMEBACKUP (excluding ~/Seafile) ---"
tar -cvpzf homebackup_$NOW.tar.gz --exclude=/home/matias/Seafile /home
echo "--- HOMEBACKUP DONE ---"
fi
EDIT: Proper syntax suggested here did the trick, thanks everyone! I'm still looking for good guides on Bash :)
As #fedorqui said, you need spaces around the brackets. That is because [ is actually a synonym for test, a real program, and so is the name of an actual command.
To make your life easier in the future, use spaces and double brackets instead ([[ and ]]). Those are handled internally by bash and are more robust against accidental errors such as forgetting the quotes around a variable substitution. Plus, && and || work as logical AND and OR in double-brackets, but not in single brackets - you have to use -a and -o instead in [ ] expressions.
Using double-brackets ("conditional expressions" in bash), you can write:
if [[ $choice = 1 || $ choice = 3 ]]; then
...
fi
and get the result you expect.
Use the following as a best practice:
if [[ $choice == 1 || $choice == 2 ]]; then
...
fi
Notice the spaces and the == for equality test.
|| and && work in double brackets. Also has some other nice features, like REGEX matching with =~ along with operators like they are known in C-like languages along with less surprises.

bash: !": event not found in Ubuntu 13.04 [duplicate]

This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
Closed 7 years ago.
I am facing the following error
bash: !": event not found
while i have write the following command
echo "HI" test.txt
in terminal,
whats the problem?
You are probably typing it as:
echo "HI!" >> test.txt
! is a special character in bash (Linux) and needs to be escaped like so:
echo "HI\!" >> test.txt
I think this question is answered very well in this link: https://serverfault.com/questions/208265/what-is-bash-event-not-found?newreg=2077048244ee45dbb6f7d1925d71458f
Hope this helps.
You have a syntax error in your command, You need to add the '>>' between your text and your name file and also try removing the quotes:
echo HI >> test.txt

add line to a file ONLY if it is not in file already [duplicate]

This question already has answers here:
Appending a line to a file only if it does not already exist
(25 answers)
Closed 1 year ago.
I want to add the following line:
nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
to the end of the file /etc/rc.d/rc.local if it does not already exist.
How can I do that from linux command line? I assume grep or sed would work, but I am not familiar enough with either to get it to work. Right now I use echo, but that just keeps adding it over and over again.
Assuming you want it at the end of the file:
LINE="nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &"
FILE=/etc/rc.d/rc.local
grep -q "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
one option is two steps:
grep -q "yourline" /path/file||sed -i '/..place../ a \the line' file
also possible to do with awk,
save all lines in array, during saving if the line was found, exit. otherwise, add the line in END{} block to the right place.
P.S. You didn't tell in the file, where to add that line.

Resources