1

I am trying to make a custom command prompt that looks like this: [][][][]$, where the [] can be filled with custom information. For example, if I write in the console . file.sh 0 2 "date -R" then the command prompt looks like this [Sat, 29 Aug 2020 11:02:40 +0200][][][]$ the 0 stands for position, and 2 stands for the type of the value (1 is string, 2 is command which is in this example, and 3 is a csv file) Basically, I want my command prompt to be dynamic, so every time I hit enter the values should be updated (not all values have to be updated, for example string stays the same all the time, or a csv column.) So when I hit enter I want my prompt go from [Sat, 29 Aug 2020 11:02:40 +0200][][][]$ to [Sat, 29 Aug 2020 11:02:45 +0200][][][]$ for example. Here is my full code:

#!/bin/bash updatedata() { v=$(awk -v strSearch="$1" ' BEGIN{ FS=";" } { gsub(/\r/,"") for(i=1;i<=NF;i++){ if($i==strSearch){ print i exit } } } ' data.csv) sum=0 for x in `cut -f $v -d ';' data.csv` do x="${x/$'\r'/}" let sum=$sum+$x done if [ $pos -eq 0 ] then v0=$sum elif [ $pos -eq 1 ] then v1=$sum elif [ $pos -eq 2 ] then v2=$sum elif [ $pos -eq 3 ] then v3=$sum fi } while [ "$#" -gt 0 ]; do pos=$1 typevar=$2 stringvar=$3 case $pos in 0) v0=$3 ;; 1) v1=$3 ;; 2) v2=$3 ;; 3) v3=$3 ;; *) echo "One of the values has invalid position entered, try again" esac case $typevar in 1) if [ $pos -eq 0 ] then if [ "$stringvar" != "null" ] then v0=$stringvar else v0="" fi elif [ $pos -eq 1 ] then if [ "$stringvar" != "null" ] then v1=$stringvar else v1="" fi elif [ $pos -eq 2 ] then if [ "$stringvar" != "null" ] then v2=$stringvar else v2="" fi elif [ $pos -eq 3 ] then if [ "$stringvar" != "null" ] then v3=$stringvar else v3="" fi fi ;; 2) if [ $pos -eq 0 ] then v0=`eval $3` elif [ $pos -eq 1 ] then v1=`eval $3` elif [ $pos -eq 2 ] then v2=`eval $3` elif [ $pos -eq 3 ] then v3=`eval $3` fi ;; 3) updatedata $3 ;; *) echo "Invalid type of variable, try again" esac shift shift shift done export PS1="[$v0][$v1][$v2][$v3]$" 

I tried using export for the PS1, didn't work. I also tried using single quoted for the PS1 like this: export PS1='[$v0][$v1][$v2][$v3]$' and that didn't work either. I also tried to do this: export PS1='[$(v0)][$(v1)][$(v2)][$(v3)]$' and that didn't work either. I don't know what to do!

example of CSV file:

Date_of_report;Municipality_code;Municipality_name;Province;Total_reported;Hospital_admission;Deceased 2020-03-13 10:00:00;GM0003;Appingedam;Groningen;0;0;0 2020-03-13 10:00:00;GM0010;Delfzijl;Groningen;0;0;0 2020-03-13 10:00:00;GM0014;Groningen;Groningen;3;0;0 2020-03-13 10:00:00;GM0024;Loppersum;Groningen;0;0;0 2020-03-13 10:00:00;GM0034;Almere;Flevoland;1;1;0 2020-03-13 10:00:00;GM0037;Stadskanaal;Groningen;0;0;0 2020-03-13 10:00:00;GM0047;Veendam;Groningen;0;0;0 2020-03-13 10:00:00;GM0050;Zeewolde;Flevoland;1;0;0 2020-03-13 10:00:00;GM0059;Achtkarspelen;Friesland;0;0;0 2020-03-13 10:00:00;GM0060;Ameland;Friesland;0;0;0 2020-03-13 10:00:00;GM0072;Harlingen;Friesland;0;0;0 2020-03-13 10:00:00;GM0074;Heerenveen;Friesland;0;0;0 

1 Answer 1

1

Your script currently only updates the prompt when it is explicitly sourced. If you want it to run every time the prompt refreshes, I think you need to use PROMPT_COMMAND.

Try the following modified script. This will call the function set_prompt to update the prompt every time. I've also exported the commands to generate the text so that they can be run again to update when you get a new prompt. Using your example command of . file.sh 0 2 "date -R", I can then see the date update when I press enter.

#!/bin/bash updatedata() { v=$(awk -v strSearch="$1" ' BEGIN{ FS=";" } { gsub(/\r/,"") for(i=1;i<=NF;i++){ if($i==strSearch){ print i exit } } } ' data.csv) sum=0 for x in `cut -f $v -d ';' data.csv` do x="${x/$'\r'/}" let sum=$sum+$x done echo $sum } while [ "$#" -gt 0 ]; do pos=$1 typevar=$2 stringvar=$3 case $pos in 0) v0=$3 ;; 1) v1=$3 ;; 2) v2=$3 ;; 3) v3=$3 ;; *) echo "One of the values has invalid position entered, try again" esac case $typevar in 1) if [ $pos -eq 0 ] then if [ "$stringvar" != "null" ] then export PROMPT0="echo $stringvar" else export PROMPT0="" fi elif [ $pos -eq 1 ] then if [ "$stringvar" != "null" ] then export PROMPT1="echo $stringvar" else export PROMPT1="" fi elif [ $pos -eq 2 ] then if [ "$stringvar" != "null" ] then export PROMPT2="echo $stringvar" else export PROMPT2="" fi elif [ $pos -eq 3 ] then if [ "$stringvar" != "null" ] then export PROMPT3="echo $stringvar" else export PROMPT3="" fi fi ;; 2) if [ $pos -eq 0 ] then export PROMPT0="exec $3" elif [ $pos -eq 1 ] then export PROMPT1="exec $3" elif [ $pos -eq 2 ] then export PROMPT2="exec $3" elif [ $pos -eq 3 ] then export PROMPT3="exec $3" fi ;; 3) if [ $pos -eq 0 ] then export PROMPT0="updatedata $3" elif [ $pos -eq 1 ] then export PROMPT1="updatedata $3" elif [ $pos -eq 2 ] then export PROMPT2="updatedata $3" elif [ $pos -eq 3 ] then export PROMPT3="updatedata $3" fi ;; *) echo "Invalid type of variable, try again" esac shift shift shift done function set_prompt() { v0=$($PROMPT0) v1=$($PROMPT1) v2=$($PROMPT2) v3=$($PROMPT3) export PS1="[$v0][$v1][$v2][$v3]$" } export PROMPT_COMMAND=set_prompt 
6
  • Hi thanks a lot for your answer, the updating command works. But the csv doesnt work anymore, i was trying to sum the values of the specific column. When i try to execute for exame . infk.sh 0 2 "date -R" 1 3 Deceased, and I press enter, the whole csv gets printed and gives me error 'error token' Commented Aug 30, 2020 at 15:34
  • @anon can you upload an example of your CSV file? Is "Deceased" a header for one of the columns? Commented Aug 30, 2020 at 15:42
  • HI, i added the example of csv to my post, deceased is a header for one of the columns yes. Commented Aug 30, 2020 at 15:46
  • @anon try my edit Commented Aug 30, 2020 at 16:00
  • Thank you so much, everything works now. You saved me haha Commented Aug 30, 2020 at 16:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.