How to pass null string in scripts - shell

How to pass null string using script. as agqmi start 0 "" "" "". if it not able find settings in profile file. And application is not invoking through script. but through command line its working (agqmi start 0 "" "" "").
profile_file
APN='airtelgprs.com'
USR='username'
PASS='password'
PAPCHAP='2'
if [ -f "$PROFILE_FILE" ]; then
echo "Loading profile..." >>$LOG
PAPCHAP=`cat agqmi-network.conf | grep 'PAPCHAP' | awk '{print $1}' | cut -f2
-d"'"`
APN=`cat agqmi-network.conf | grep 'APN' | awk '{print $1}' | cut -f2 -d"'"`
USR=`cat agqmi-network.conf | grep 'USR' | awk '{print $1}' | cut -f2 -d"'"`
PASS=`cat agqmi-network.conf | grep 'PASS' | awk '{print $1}' | cut -f2 -d"'"`
if [ "x$PAPCHAP" == "x" ]; then
PAPCHAP="0"
fi
if [ "x$APN" == "x" ]; then
APN="\"\""
fi
if [ "x$USR" == "x" ]; then
USR="\"\""
fi
if [ "x$PASS" == "x" ]; then
PASS="\"\""
fi
fi
i tried to execute
STATUS_CMD="./agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS""
echo "$STATUS_CMD" >>$LOG
`$STATUS_CMD`

The way to run your command in a way that you store it first is through this (use arrays):
STATUS_CMD=(./agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS")
echo "${STATUS_CMD[*]}" >>$LOG
"${STATUS_CMD[#]}"
You could also use eval but it could misinterpret it depending on the values of your variables.
And you probably no longer need to re-assign your variables that's meant to be empty to "" (literal). Only the one that needs to be converted to 0:
if [ "x$PAPCHAP" == "x" ]; then
PAPCHAP="0"
fi
#if [ "x$APN" == "x" ]; then
# APN="\"\""
#fi
#if [ "x$USR" == "x" ]; then
# USR="\"\""
#fi
#if [ "x$PASS" == "x" ]; then
# PASS="\"\""
#fi
And your comparisons need no markers like x. Using [[ ]] is also recommended.
if [[ $PAPCHAP == '' ]]; then ## Or simply [[ -z $PAPCHAP ]]
PAPCHAP=0
fi
Update for POSIX:
if [ -z "$PAPCHAP" ]; then
PAPCHAP=0
fi
#if [ -z "$APN" ]; then
# APN=''
#fi
#if [ -z "$USR" ]; then
# USR=''
#fi
#if [ -z "$PASS" ]; then
# PASS=''
#fi
STATUS_CMD="./agqmi start \"$PAPCHAP\" \"$APN\" \"$USR\" \"$PASS\""
echo "$STATUS_CMD" >>"$LOG"
./agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS" ## Just execute it directly and not inside a variable.
And maybe you should not add ./?
STATUS_CMD="agqmi start \"$PAPCHAP\" \"$APN\" \"$USR\" \"$PASS\""
echo "$STATUS_CMD" >>"$LOG"
agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS"
You actually don't need to store it on a variable anyway:
echo "agqmi start \"$PAPCHAP\" \"$APN\" \"$USR\" \"$PASS\"" >>"$LOG"
agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS"

Did you try passing "\0" as parameter?
I have a solaris server and whenever I need to pass NULL string as parameter I use "\0".
Your command would look like
agqmi start 0 "\0" "\0" "\0"
Please let me know if it works for you.

Related

Ip host script bash

In the first if we want the hostname to appear, which is the 5th field from a file. Then if the IP we give to the host command does not exist, then the command returns message 3 (NXDOMAIN). The script should recognize if the command was "not found". In this case it will
must simply print (-).
#!/bin/bash
ip="$1"
if [ "$ip" ] ; then
host "$ip" | cut -d' ' -f5
elif
[[ "$ip" =~ "[3(NXDOMAIN)]$" ]] ; then
echo "-"
fi
Do u have any solution on this exercise?
You're not testing the result of the host command, you're testing the value of the original $ip variable.
Save the output to a variable, test that variable, then either print the output or - depending on the test.
You don't need to do a regexp match, just match the exact string.
#!/bin/bash
ip="$1"
if [ "$ip" ] ; then
result=$(host "$ip" | cut -d" " -f5)
if [[ $result = "3(NXDOMAIN)" ]] ; then
echo "-"
else
echo "$result"
fi
fi
The answer is much simpler than you think, you don't need to do any matching. You can just use the return code from host
#!/bin/bash
ip="$1"
if domain=$(host "$1"); then
echo "${domain##* }"
else
echo "-"
fi
Proof of Concept
$ testHost(){ if domain=$(host "$1"); then echo "${domain##* }"; else echo "-"; fi }
$ testHost 172.217.6.46
sfo03s08-in-f14.1e100.net.
$ testHost 172.217.6.466
-
#!/bin/bash
if [ -n "$1" ] && [[ $1 =~ ^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ ]] ;then
res=$(host "$1" | cut -d' ' -f5)
if [ "$res" != "3(NXDOMAIN)" ]; then
echo "$res"
else
echo "-"
fi
else
echo "please enter a valid ip"
fi
if you want to cover also ipv6 then I think this will cover it
#!/bin/bash
# ipv4
if [[ $1 =~ ^([[:digit:]]{1,2}|1[[:digit:]][[:digit:]]|2[0-4][[:digit:]]|25[0-5])\.([[:digit:]]{1,2}|1[[:digit:]][[:digit:]]|2[0-4][[:digit:]]|25[0-5])\.([[:digit:]]{1,2}|1[[:digit:]][[:digit:]]|2[0-4][[:digit:]]|25[0-5])\.([[:digit:]]{1,2}|1[[:digit:]][[:digit:]]|2[0-4][[:digit:]]|25[0-5])$ ]]; then
res=`host "$1" | cut -d' ' -f5`
if [ "$res" != "3(NXDOMAIN)" ]; then
echo "$res"
else
# valid ipv4 IP but not connected
echo "-"
fi
# ipv6
elif [[ $1 =~ ^(([[:xdigit:]]{1,4}:){7,7}[[:xdigit:]]{1,4}|([[:xdigit:]]{1,4}:){1,7}:|([[:xdigit:]]{1,4}:){1,6}:[[:xdigit:]]{1,4}|([[:xdigit:]]{1,4}:){1,5}(:[[:xdigit:]]{1,4}){1,2}|([[:xdigit:]]{1,4}:){1,4}(:[[:xdigit:]]{1,4}){1,3}|([[:xdigit:]]{1,4}:){1,3}(:[[:xdigit:]]{1,4}){1,4}|([[:xdigit:]]{1,4}:){1,2}(:[[:xdigit:]]{1,4}){1,5}|[[:xdigit:]]{1,4}:((:[[:xdigit:]]{1,4}){1,6})|:((:[[:xdigit:]]{1,4}){1,7}|:)|fe80:(:[[:xdigit:]]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[[:digit:]]){0,1}[[:digit:]])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[[:digit:]]){0,1}[[:digit:]])|([[:xdigit:]]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[[:digit:]]){0,1}[[:digit:]])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[[:digit:]]){0,1}[[:digit:]]))$ ]]; then
res=`host "$1" | cut -d' ' -f5`
if [ "$res" != "3(NXDOMAIN)" ]; then
echo "1. $res"
else
# valid ipv6 IP but not connected
echo "2. -"
fi
else
echo "Please enter a valid IP"
fi
Note: For some versions of bash the -4 and -6 options do not work.
Thanks to Léa Gris for pointing out the locales problem.
Inspired from https://helloacm.com/how-to-valid-ipv6-addresses-using-bash-and-regex/

String to string replacement in file while taking those string from a file

I have following requirement to be written in bash, which is as follows
I have file consist of filename,stra,strb,colname
filename: Name of the file
stra: string to be find out
strb: string to be replaced with
colname: is name of the column in file
stringlist.csv:
filename,stra,strb,colname
x.txt,aaa,xxx,col1
y.txt,bbb,yyy,col1
x.txt:
col1,col2,col3
aaa,10,20
bbb,20,30
aaa,21,34
ccc,43,98
y.txt:
aaa,10,20
bbb,20,30
aaa,21,34
ccc,43,98
From the stringlist.csv as input file, In x.txt column col1 aaa should be replaced with xxx and respectively in y.txt col1 containing bbb should be replaced with yyy and other content in the file should remain same
I did it by myself. I was expecting some hint but not the code
echo -n "Enter 1 for directory
Enter 2 for list of files
Enter 3 for one file as input: "
read mode
if [ $mode -eq 1 ]; then
echo -n "Enter direcotry path: "
read dir
DIRS=`ls -l $dir | awk '{print $9}'`
echo -n "Do you want to override the file or want to create a new file(y/n)? "
read choice
if [ $choice == "n" ]; then
echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
read prefix
elif [ $choice == "y" ]; then
:
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
elif [ $mode -eq 2 ]; then
echo -n "Enter List of files separated by space: "
read DIRS
echo -n "Do you want to override the file or want to create a new file(y/n)? "
read choice
if [ $choice == "n" ]; then
echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
read prefix
elif [ $choice == "y" ]; then
:
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
elif [ $mode -eq 3 ]; then
echo -n "Enter file name: "
read DIRS
echo -n "Do you want to override the file or want to create a new file(y/n)? "
read choice
if [ $choice == "n" ]; then
echo -n "Enter the prefix of the output file. Output file will be prefix followed by filename: "
read prefix
elif [ $choice == "y" ]; then
:
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
else
echo $'You have chosen invalid option.... Bye!!\n'
exit
fi
echo -n "Enter file delimiter: "
read del
echo -n "enter string replacement file: "
read strrepfile
echo -n "Enter Delimiter for string replacement input file: "
read indel
if [ $indel == "tab" ]; then
indel='\t'
sed "s/$indel/|/g" $strrepfile > $strrepfile.1
else
sed "s/$indel/|/g" $strrepfile > $strrepfile.1
fi
#filenames=`awk 'BEGIN{FS=OFS='$indel1'}{print $1}' $strrepfile`
if [ $del == "tab" ]; then
del1='"'"\t"'"'
else
del1='"'$del'"'
fi
for var in $DIRS; do
if [ $mode -eq 1 ]; then
colheadfile=$dir/$var
else
colheadfile=$var
fi
break
done
awvar="-v col"
while read -r line
do
echo "$line" > temp
stra=`awk -F '|' '{print $1}' temp`
strb=`awk -F '|' '{print $2}' temp`
col=`awk -F '|' '{print $3}' temp`
if [ $del == "tab" ]; then
del='\t'
fi
filedel="'"$del"'"
colpos=`head -1 $colheadfile | tr -s $filedel '\n' | nl -nln | grep $col | cut -f1`
colpos=$(echo $colpos | tr -d ' ')
awkvar1=$awkvar1$awvar$colpos'='$colpos' '
gsubvar=$gsubvar'gsub("'$stra'","'$strb'",$'$colpos');'
done < "$strrepfile.1"
echo $awkvar1
echo $gsubvar
for dir1 in $DIRS; do
echo $dir1
if [ $mode -eq 1 ] && [ $choice == "y" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir/$dir1 > $dir1
elif [ $mode -eq 1 ] && [ $choice == "n" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir/$dir1 > $prefix'_'$dir1
elif [ $mode -eq 2 ] && [ $choice == "y" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $dir1
elif [ $mode -eq 2 ] && [ $choice == "n" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $prefix'_'$dir1
elif [ $mode -eq 3 ] && [ $choice == "y" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $dir1
elif [ $mode -eq 3 ] && [ $choice == "n" ]; then
awk $awkvar1 'BEGIN{FS=OFS='$del1'}{'$gsubvar'print;}' $dir1 > $prefix'_'$dir1
fi
done
rm $strrepfile.1
rm temp
Thanks

Bash script freezes when sent SIGINT

I finally grew tired of makefiles and wrote my own bash script to do my compiling. I wrote the whole thing, it works great, but for some reason, it freezes sometimes when I try to cancel it with ctrl-c. Here's the script:
#!/bin/bash
# Just to see if the script even sees the SIGINT
trap caught SIGINT
caught() { echo "hi"; }
compile() {
cpp=$(echo "$1" | sed -e 's/$/.cpp/' -e 's/^/src\//')
o=$(echo "$1" | sed -e 's/$/.o/' -e "s/^/$build_dir\//")
echo "$compile -c $cpp -o $o"
eval "$compile -c $cpp -o $o"
return $?
}
# I know this isn't normal, but I hate it when I forget to include something
# in the header and it fails way down the line
compile_h() {
h=$(echo "$1" | sed -e 's/$/.h/' -e 's/^/src\//')
o=$(echo "$1" | sed -e 's/$/.o/' -e "s/^/$build_dir\/headers\//")
echo "$compile -c $h -o /dev/null"
eval "$compile -x c++ -c $h -o $o"
if [ $? -ne 0 ]; then
return 1
fi
rm "$o"
return 0
}
build_type=$(awk 'NR==1' .build_options)
compile_command_debug=$(awk 'NR==2' .build_options)
link_command_debug=$(awk 'NR==3' .build_options)
compile_command_production=$(awk 'NR==4' .build_options)
link_command_production=$(awk 'NR==5' .build_options)
libraries=$(awk 'NR==6' .build_options)
# Make options for this build
build_dir="build"
compile="$compile_command_debug"
link="$link_command_debug"
if [ "$build_type" == "production" ]; then
build_dir="buildp"
compile="$compile_command_production"
link="$link_command_production"
fi
# These options need to be changeable later
output="game"
job_number=5
# There are more options, but they aren't important for this problem
while [ "$1" != "" ]; do
if [ "$1" == "clean" ]; then
rm -r $build_dir/*
fi
shift
done
# Get filenames
cpps=$(find src -name *.cpp | sed -e 's/src\///' -e 's/.cpp//' | sort)
hs=$(find src -name *.h | sed -e 's/src\///' -e 's/.h//' | sort)
# Ensure that all directories exist
directories=$(find src -type d | tail --lines=+2 | sed 's/src\///' | sort)
if [ ! -d "$build_dir/headers" ]; then
mkdir "$build_dir/headers"
fi
for dir in $directories; do
if [ ! -d "$build_dir/$dir" ]; then
mkdir "$build_dir/$dir"
fi
if [ ! -d "$build_dir/headers/$dir" ]; then
mkdir "$build_dir/headers/$dir"
fi
done
all_o="" # To be used for linking
# Determine what files need to be compiled
cpp_needed=""
h_needed=""
link_needed=false
# Check cpp files
for cpp_base in $cpps; do
o=$(echo "$cpp_base" | sed -e 's/$/.o/' -e "s/^/$build_dir\//")
all_o="$all_o $o"
d_file=$(echo "$cpp_base" | sed -e 's/$/.d/' -e "s/^/$build_dir\//")
if [ -f "$d_file" ]; then
d=$(<"$d_file")
d=$(echo "$d" | tr " " "\n" | tail --lines=+2 | grep "s")
if [ "$link_needed" = false ]; then
if [ "$o" -nt "$output" ]; then
link_needed=true
fi
fi
for dep in $d; do
if [ "$dep" -nt "$o" ]; then
if [ "$cpp_needed" == "" ]; then cpp_needed="$cpp_base"
else cpp_needed="$cpp_needed $cpp_base"
fi
link_needed=true
break
fi
done
else
if [ "$cpp_needed" == "" ]; then cpp_needed="$cpp_base"
else cpp_needed="$cpp_needed $cpp_base"
fi
link_needed=true
fi
done
# Check h files
for h_base in $hs; do
d_file=$(echo "$h_base" | sed -e 's/$/.d/' -e "s/^/$build_dir\/headers\//")
if [ -f "$d_file" ]; then
d=$(<"$d_file")
d=$(echo "$d" | tr " " "\n" | tail --lines=+2 | grep "s")
for dep in $d; do
if [ "$dep" -nt "$d_file" ]; then
if [ "$h_needed" == "" ]; then h_needed="$h_base"
else h_needed="$h_needed $h_base"
fi
break
fi
done
else
if [ "$h_needed" == "" ]; then h_needed="$h_base"
else h_needed="$h_needed $h_base"
fi
fi
done
# Compile
did_something=false
# Compile hs
while [ "$h_needed" != "" ]; do
for index in $(seq 1 $job_number); do
if [ "$h_needed" == "" ]; then break; fi
if ! kill -0 ${pids[index]} 2>/dev/null; then
new_file=$(echo "$h_needed" | awk '{print $1;}')
if [ $(echo "$h_needed" | wc -w) -eq 1 ]; then h_needed=""
else h_needed=$(echo "$h_needed" | cut -d " " -f2-)
fi
compile_h "$new_file" &
pids[index]=$!
did_something=true
fi
done
wait -n
if [ $? -ne 0 ]; then
wait
exit 1
fi
done
while [ $(pgrep -c -P$$) -gt 0 ]; do
wait -n
if [ $? -ne 0 ]; then
wait
exit 1
fi
done
# Compile cpps
while [ "$cpp_needed" != "" ]; do
for index in $(seq 1 $job_number); do
if [ "$cpp_needed" == "" ]; then break; fi
if ! kill -0 ${pids[index]} 2>/dev/null; then
new_file=$(echo "$cpp_needed" | awk '{print $1;}')
if [ $(echo "$cpp_needed" | wc -w) -eq 1 ]; then cpp_needed=""
else cpp_needed=$(echo "$cpp_needed" | cut -d " " -f2-)
fi
compile "$new_file" &
pids[index]=$!
did_something=true
fi
done
wait -n
if [ $? -ne 0 ]; then
wait
exit 1
fi
done
while [ $(pgrep -c -P$$) -gt 0 ]; do
wait -n
if [ $? -ne 0 ]; then
wait
exit 1
fi
done
# Compile program
if [ "$link_needed" = true ]; then
echo "$link $all_o -o game $libraries"
eval "$link $all_o -o game $libraries"
did_something=true
fi
# Make a message if nothing compiled
if [ "$did_something" = false ]; then
echo "Program is already compiled."
fi
It normally works perfectly. However, sometimes, when I try to cancel it with ctrl-c it just freezes. With a bit of debugging I saw that when the script wasn't setting up a new job, ctrl-c would work just fine. But when it was in the middle of setting up a new job, it would freeze the script. It wouldn't even catch the SIGINT (which that "echo hi" thing is for at the top). I honestly have no idea what's going on. Does anybody know what's going on? Thank you!
Edit: I realized I should probably mention I use g++ to compile.
Edit again: Here's an even-more stripped down version of the script. You would still need to setup some files to compile if you wanted to test it:
#!/bin/bash
# Just to see if the script even sees the SIGINT
trap caught SIGINT
caught() { echo "hi"; }
# I know this isn't normal, but I hate it when I forget to include something
# in the header and it fails way down the line
compile_h() {
h=$(echo "$1" | sed -e 's/$/.h/' -e 's/^/src\//')
o=$(echo "$1" | sed -e 's/$/.o/' -e "s/^/$build_dir\/headers\//")
echo "$compile -c $h -o /dev/null"
eval "$compile -x c++ -c $h -o $o"
if [ $? -ne 0 ]; then
return 1
fi
rm "$o"
return 0
}
build_type="debug"
build_dir="build"
compile="g++"
job_number=5
# Get filenames
hs=$(find src -name *.h | sed -e 's/src\///' -e 's/.h//' | sort)
h_needed=$(echo $hs)
# Compile hs
while [ "$h_needed" != "" ]; do
for index in $(seq 1 $job_number); do
if [ "$h_needed" == "" ]; then break; fi
if ! kill -0 ${pids[index]} 2>/dev/null; then
new_file=$(echo "$h_needed" | awk '{print $1;}')
if [ $(echo "$h_needed" | wc -w) -eq 1 ]; then h_needed=""
else h_needed=$(echo "$h_needed" | cut -d " " -f2-)
fi
compile_h "$new_file" &
pids[index]=$!
did_something=true
fi
done
wait -n
if [ $? -ne 0 ]; then
wait
exit 1
fi
done
while [ $(pgrep -c -P$$) -gt 0 ]; do
wait -n
if [ $? -ne 0 ]; then
wait
exit 1
fi
done
Any program that you run in your script may override your trap and set up its own. That trap might for example crash the currently running program for some reason. When this happens, take a look at the process tree in ps wafux to find the most likely culprit. For example, a zombie (Z) or uninterruptible sleep (D) process state (see man ps) is common when a process isn't going anywhere.

Why does my bash prompt sometimes get overwritten?

Here's the relevant parts of my bashrc:
function find_git_branch {
local dir=. head
until [ "$dir" -ef / ]; do
if [ -f "$dir/.git/HEAD" ]; then
head=$(< "$dir/.git/HEAD")
if [[ $head == ref:\ refs/heads/* ]]; then
git_branch=" (${head#*/*/})"
elif [[ $head != '' ]]; then
git_branch=' (detached)'
else
git_branch=' (unknown)'
fi
return
fi
dir="../$dir"
done
git_branch=''
}
function shortpath {
# How many characters of the $PWD should be kept
local pwd_length=40
local lpwd="${PWD/#$HOME/~}"
if [ $(echo -n $lpwd | wc -c | tr -d " ") -gt $pwd_length ]
then newPWD="...$(echo -n $lpwd | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
else newPWD="$(echo -n $lpwd)"
fi
echo $newPWD
}
PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
# PS1 prompt color vars
CYAN="\e[36m"
RED="\e[31m"
GREEN="\e[32m"
DEFAULT="\e[0m"
TIME="[\t]"
DIRNAME="\w"
export PS1="\u#\h:\[$CYAN\]\$(shortpath)\[$GREEN\]\[\$git_branch\]\[$DEFAULT\] \$ "
It works well, but sometimes as I type or hit the up arrow for previous commands, part of the prompt gets overwritten in the terminal. Why does this happen?
Looks like you're including your $git_branch part in a non-printing-chars block (\[...\]).

Coloring not colored text in Bash

I have to colorize some words in the text, that works good, but I have a problem when its already colored. When its colored I dont want to colour it again with different color. My problem is that my code color it again even if it is already colored.
Here is my code:
var=$(echo -e $line | grep ".*[^m]${word}[^\][^e].*" | sed -e "s/${word}/${color}${word}${endColor}/g")
if(var -n);then
line=$var
f
Its a script where is every odd the color and even is word you want to color. The problem is when there is a word that is already colored and I dont want to recolore it.
Input could be anything
here is full code
function GetColor {
if [ $1 == 'r' ];then
color=$red;
fi
if [ $1 == 'b' ];then
color=$blue;
fi
if [ $1 == 'g' ];then
color=$green;
fi
}
red=$'\e[31m'
green=$'\e[32m'
blue=$'\e[34m'
endColor=$'\e[0m'
a=0
color=""
word=""
while read input
do
radek=$input
for i in $*; do
if (( a% 2 )); then
word=$i
var=$(echo -e $line | grep ".*[^m]${word}[^\][^e].*" | sed -e "s/${word}/${color}${word}${endColor}/g")
if(var -n);then
line=$var
fi
else
color=""
GetColor "$i"
fi
let "a += 1"
done
echo -e $line
exit
done
thanks for help
This is my version, but I've just made the script run without errors. I'm not sure what the problem is, but the colour of already coloured words are not changed.
I suspect
if(var -n);then
I corrected it to
if [ -n "$var" ]; then
Here's the script
function GetColor {
if [ $1 == 'r' ];then
color=$red;
fi
if [ $1 == 'b' ];then
color=$blue;
fi
if [ $1 == 'g' ];then
color=$green;
fi
}
red=$'\e[31m'
green=$'\e[32m'
blue=$'\e[34m'
endColor=$'\e[0m'
a=0
color=""
word=""
while read input
do
line=$input
for i in $*; do
if (( a% 2 )); then
word=$i
var=$(echo -e $line | grep ".*[^m]${word}[^\][^e].*" | sed -e "s/${word}/${color}${word}${endColor}/g")
if [ -n "$var" ]; then
line=$var
fi
else
color=""
GetColor "$i"
fi
let "a += 1"
done
echo -e $line
exit
done

Resources