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