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 [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            v0=$stringvar
        else
            v0=&quot;&quot;
        fi
    elif [ $pos -eq 1 ]
    then
        if [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            v1=$stringvar
        else
            v1=&quot;&quot;
        fi
    elif [ $pos -eq 2 ]
    then
        if [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            v2=$stringvar
        else
            v2=&quot;&quot;
        fi
    elif [ $pos -eq 3 ]
    then
        if [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            v3=$stringvar
        else
            v3=&quot;&quot;
        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 &quot;Invalid type of variable, try again&quot;



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
dsv2001
  • 13

1 Answers1

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 [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            export PROMPT0=&quot;echo $stringvar&quot;
        else
            export PROMPT0=&quot;&quot;
        fi
    elif [ $pos -eq 1 ]
    then
        if [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            export PROMPT1=&quot;echo $stringvar&quot;
        else
            export PROMPT1=&quot;&quot;
        fi
    elif [ $pos -eq 2 ]
    then
        if [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            export PROMPT2=&quot;echo $stringvar&quot;
        else
            export PROMPT2=&quot;&quot;
        fi
    elif [ $pos -eq 3 ]
    then
        if [ &quot;$stringvar&quot; != &quot;null&quot; ]
        then
            export PROMPT3=&quot;echo $stringvar&quot;
        else
            export PROMPT3=&quot;&quot;
        fi
    fi ;;


  2) if [ $pos -eq 0 ]
    then
        export PROMPT0=&quot;exec $3&quot;
    elif [ $pos -eq 1 ]
    then
        export PROMPT1=&quot;exec $3&quot;
    elif [ $pos -eq 2 ]
    then
        export PROMPT2=&quot;exec $3&quot;
    elif [ $pos -eq 3 ]
    then
        export PROMPT3=&quot;exec $3&quot;
    fi ;;

  3) if [ $pos -eq 0 ]
    then
        export PROMPT0=&quot;updatedata $3&quot;
    elif [ $pos -eq 1 ]
    then
        export PROMPT1=&quot;updatedata $3&quot;
    elif [ $pos -eq 2 ]
    then
        export PROMPT2=&quot;updatedata $3&quot;
    elif [ $pos -eq 3 ]
    then
        export PROMPT3=&quot;updatedata $3&quot;
    fi ;;

  *) echo &quot;Invalid type of variable, try again&quot;


esac
shift
shift
shift

done

function set_prompt() { v0=$($PROMPT0) v1=$($PROMPT1) v2=$($PROMPT2) v3=$($PROMPT3)

export PS1=&quot;[$v0][$v1][$v2][$v3]$&quot;

}

export PROMPT_COMMAND=set_prompt

  • 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' – dsv2001 Aug 30 '20 at 15:34
  • @anon can you upload an example of your CSV file? Is "Deceased" a header for one of the columns? – Matt Spicer Aug 30 '20 at 15:42
  • HI, i added the example of csv to my post, deceased is a header for one of the columns yes. – dsv2001 Aug 30 '20 at 15:46
  • @anon try my edit – Matt Spicer Aug 30 '20 at 16:00
  • Thank you so much, everything works now. You saved me haha – dsv2001 Aug 30 '20 at 16:21
  • @anon you're welcome! If I answered your question, please upvote and accept my answer :) – Matt Spicer Aug 30 '20 at 16:57