Run function on every prompt line with .bash_profile editing - bash

I have the following PS1 command in my .bash_profile:
PS1="$(svn info 2>&1 | grep 'Relative URL' | awk '{print $NF}')"
So that the output of this command is presented in the prompt line.
But it is run once I start the terminal and it just stays there, instead of changing while I navigate through my directories. So it runs once and is left there.
How can I make it change as I am navigating my directories?

PROMPT_COMMAND
If set, the value is executed as a command prior to issuing each
primary prompt.
$ PROMPT_COMMAND=date
Sun Feb 21 13:35:21 EST 2016
$ echo a
a
Sun Feb 21 13:35:23 EST 2016
$ echo b
b
Sun Feb 21 13:35:24 EST 2016
$ PROMPT_COMMAND='PS1=`date +%H:%M`\ $\ '
13:35 $ sleep 60
13:36 $

Related

linux touch relative time giving wrong result

I'm using bash touch to change the date and time of a file. It works correctly if I simply specify the date and time. If, however, I use a relative time I get unexpected behavior.
$ date -R -r test.txt
Sat, 28 May 2022 02:56:22 -0400
$ touch -d '27 May 2022 05:31:12' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 05:31:12 -0400
$ touch -d '27 May 2022 05:31:12 - 1 hour' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 03:31:12 -0400
$ touch -d "27 May 2022 05:31:12 + 1 hour" test.txt
$ date -R -r test.txt
Fri, 27 May 2022 01:31:12 -0400
Note that although I asked for 1 hour earlier and then 1 hour later, it gave 2 hours earlier and 4 hours earlier respectively.
Any help would be appreciated.
When you do your touch, you do no specify the timezone. If you do, all your date arithmetic works ok.
EX:
$ touch -d '27 May 2022 05:31:12' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 05:31:12 -0400
$
$ touch -d '27 May 2022 05:31:12 -0400 - 1 hour' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 04:31:12 -0400

Close tmux session with a particular name

I would like to have a command that closes all tmux session which name corresponds to a pattern. The pattern, in the language of regex, is nn\d+
I feel that I should be able to use a combination of grep and xargs.
I tried with
tmux ls | grep -P "nn\d+" | ..
but I am not sure how to use xargs here, that is: I am not sure how to refer to only the name part of the matched string, and to one string at the time.
To be more precise, the output of tmux ls is something like:
1: 1 windows (created Thu Mar 25 12:49:17 2021)
2: 1 windows (created Thu Mar 25 12:50:20 2021)
nn312133: 1 windows (created Thu Mar 25 12:53:54 2021)
nn3123: 1 windows (created Thu Mar 25 12:53:52 2021)
The output from grep is:
nn312133: 1 windows (created Thu Mar 25 12:53:54 2021)
nn3123: 1 windows (created Thu Mar 25 12:53:52 2021)
I should need to pipe nn312133 and nn3123 into tmux kill-session -t $x
Any idea? Thanks
Use grep -Po to make grep only print out the matched part instead of the whole line.
The answer is tmux ls | grep -Po "nn\d+" | xargs -n1 tmux kill-session -t

Bash file does not run by directory in ubuntu

When I run a bash file inside its directory in ubuntu there isn't any problem
But when I try to run it by its directory I get the error:
sh: 0: Can't open /directory/file.sh
I followed this steps:
$ cat web/test.sh
#!/bin/bash
touch /tmp/testweb
echo "File has been created succesfully"
$ ls -la web/test.sh
-rwxr--r-- 1 test test 88 Dec 5 05:58 web/test.sh
1- $ date
Sat 05 Dec 2020 06:00:14 AM UTC
2- $ /home/test-env/web/test.sh
File has been created succesfully
3- $ ls -la /tmp/test*
-rw-r--r-- 1 test test 0 Dec 5 06:00 /tmp/testweb
Able to run the file in ubuntu and debian as well

No output from bash -c over ssh

How come when I run this on my local machine I get output
$ bash -c 'a=$(date) && echo $a'
Thu Feb 20 23:12:26 MST 2014
but if I try it over ssh (I have a public key on the other box, but no forced commands in authorized_keys)
$ ssh nathan#gnunix bash -c 'a=$(date) && echo $a'
Just a blank line is printed?
You don't need bash -c probably, just this would be able to print date:
ssh nathan#gnunix 'a=$(date) && echo $a'
If you must use bash -c then escape $ like this (otherwise $ is interpreted by current shell not remote one)
ssh nathan#gnunix "bash -c 'a=\$(date) && echo \$a'"
Fri Feb 21 01:22:42 EST 2014

display a line every two line (osX) zsh

I'd to display every two line, a line from a file. I've seen the sed -n 'f~d' awk and perl method. But the sed one doesn't work on osX (As I understood) and the two others are are interpreted languages which i can't use.
Can you help me ?
Here's an exemple :
output before :
-rw-r--r-- 1 mfassi-f 2013 22 Jul 17 12:36 test.sh
-rw-r--r-- 1 mfassi-f 2013 29 Jul 17 12:30 test1.sh
-rw-r--r-- 1 mfassi-f 2013 22 Jul 17 12:36 test2.sh
-rw-r--r-- 1 mfassi-f 2013 29 Jul 17 12:30 test3.sh
-rw-r--r-- 1 mfassi-f 2013 22 Jul 17 12:36 test4.sh
-rw-r--r-- 1 mfassi-f 2013 29 Jul 17 12:30 test5.sh
-rw-r--r-- 1 mfassi-f 2013 22 Jul 17 12:36 test6.sh
output after :
-rw-r--r-- 1 mfassi-f 2013 29 Jul 17 12:30 test1.sh
-rw-r--r-- 1 mfassi-f 2013 29 Jul 17 12:30 test3.sh
-rw-r--r-- 1 mfassi-f 2013 29 Jul 17 12:30 test3.sh
-rw-r--r-- 1 mfassi-f 2013 29 Jul 17 12:30 test5.sh
Here are two answers. One for a file, and one for command-line input.
['cause the question's changed ever so slightly, but these two seemed too similar to put as independent answers].
You can use zsh, ls, cut and paste to do this in a for loop. It's not the cleanest solution, but it does work (surprisingly).
for file in `ls -1 | paste - - | cut -f 1`
do
ls -l -d $file
done
We take the output of ls -1, then extract every second filename. (The way ls chooses to sort the files will have an impact here). Then, we do ls -l -d on each of these files. -d is necessary to stop ls from showing us the contents of $file, if $file is a directory. (Not sure if this is OS X specific, or if that's default POSIX ls behaviour).
Second answer: display every second line from a file.
If you're after a mostly zsh solution, you could do something like the following:
$ jot 8 0 7 >> sample.txt # Generate some numbers.
$ count=0 # Storage variable
$ for i in `cat sample.txt`
do
if [ $(( $count % 2 )) -eq 0 ] ; then
echo $i
fi
count=`expr $count + 1`
done
This displays every second line.
Notes:
- This leaves a variable count in your session afterwards (it's not clean).
- This fails badly if sample.txt does not contain a single word per line.
- I'm almost sure that the modulus comparison I do isn't the most efficient: I grabbed it from here.
- I say it's mostly zsh because it does rely on cat, but I'm not sure how to avoid that.
The OS X version of sed is frustrating. Using sed -n '0~2p' <filename> doesn't work because, in the BSD sed, -n does something different:
-n
By default, each line of input is echoed to the standard output after all of the commands have been applied to it. The -n option suppresses this behavior.
I'd highly recommend installing GNU sed, which can be done using Homebrew:
brew install gnu-sed
And then you can use:
gsed -n '0~2p' filename # Display the 2nd, 4th etc
gsed -n '1~2p' filename # Display the 1st, 3rd etc.

Resources