I am trying to write an if statement which checks if the first two characters of particular variable starts with '$$'. I have tried the following but the if condition is always evaluating to true.
var1=$$abc
var2=$$def
var1_cut=`echo $var1 | cut -c -2`
var2_cut=`echo $var2 | cut -c -2`
if [[ $var1_cut == "$$" && $var2_cut== "$$" ]]
then
break
else
echo $var1
fi
Could anyone help me out here. Is there anything to watch out for while trying to match $$ in if statements
Use single quotes to avoid expansion and double while comparing.
If you are using bash, you don't need to use cut. You can do that in bash itself.
var1='$$abc'
var2='$$def'
var1_cut=${var1:0:2}
var2_cut=${var2:0:2}
if [[ "$var1_cut" == '$$' && "$var2_cut" == '$$' ]]
then
# Do something
else
echo "$var1"
fi
Related
In zsh we can test for the existence of a progragram using this:
(( $+commands[program] )) && program
My question is: How to use commands for testing the existence of two programs instead one?
Found this under Subscript Flags in man 1 zshparam (portions removed, emphasis added):
r if this flag is given, the exp is taken as a pattern
i Like r,
I Like i, but gives the index of the last match, or all possible matching keys in an associative array. On failure substitutes 0, or the empty string for an associative array. This flag is best when testing for values or keys that do not exist.
So to test for both program1 and program2, you can do:
(( $#commands[(I)(program1|program2)] == 2 ))
I guess you could just use the following:
if ((( $+commands[program1] ) && ( $+commands[program2] ))); then
echo 'both command exists'
fi
the same could be done using bash:
if [[ `command -v program1` && `command -v program2`]]; then
echo 'both command exists'
fi
I figured out one way of solving this issue:
alias rm='echo use trash instead!'
(( $+commands[trash] && $+commands[gio] )) && alias trash='gio trash'
I still would like to know if it is possible to use something like:
(( $+commands[program1 program2] ))
I need a regular expression that I can use in match() function to see if value given at command line argument exists at the end of a given string.
I am using awk then trying use match function to get above result:
while read line; do
cat $line | awk -v value="$2.$" '{ if ( match("$1,value) != 0 ) print match("arvind","ind.$") " " "arvind" }'
done < xreffilelist.txt
My input will be as: 04.02.03.00
and my other list will be 04.02.01.00, 04.02.02.00 & 04.02.05.00.
After comparing the input(04.02.03.00) with the given list if the exact match(04.02.03.00) is not in the list then 04.02.02.00 should return as output.
You can use the following as an example with bash
read INPUT
for STRING in 04.02.01.00 04.02.02.00 04.02.05.00
do
if [[ "$INPUT" = $STRING ]]
then
FOUND=1
fi
done
if [[ FOUND -ne 1 ]]
then
echo 04.02.02.00
fi
i agree with Michael just in order that to work i think it would need the "test", so like this. the brackets would help you though protect urself from null variables but thats matter of opinion.
read INPUT
for STRING in 04.02.01.00 04.02.02.00 04.02.05.00
do
if test "$INPUT" = $STRING
then
FOUND=1
fi
done
if test $FOUND -ne 1
then
echo 04.02.02.00
fi
I am trying to find the number of fields that contain the word entered by the user. I do not know the syntax to use the variable in my awk statement. If I just use a literal string of $i == "Washington" it works, but I need it to use the input. When I try this it returns nothing:
<code>
read Choice
awk '{
for (i=1;i<=NF;i++)
if ( $i == "$Choice")
c++
}
END{
print c}' DC_Area.csv
</code>
Shell variables are not visible in awk. That's why the code in the OP doesn't work. Use the -v option to pass on shell variables to awk.
Try
read Choice
awk -v Choice="$Choice" '{
for (i=1;i<=NF;i++)
if ( $i == Choice)
c++
}
END{
print c}' DC_Area.csv
For some reason this script will work with all of the 'echo's at the end, but without them $wall is an empty string. This seems like really odd behaviour.
#!/bin/zsh
if [ ! -n "$1" ] ; then
files=(~/pictures/backgrounds/*jpg)
else
while [ $1 ] ; do
files+=(`echo $1/*jpg`)
shift
done
fi
echo $files
N=${#files}
echo $N
((N=RANDOM%N))
echo $N
wall=${files[$N]}
echo $wall
cp $wall ~/wall.jpg
This code will sometimes fail because RANDOM%N can result in zero and zsh array indexes start with 1. You should use RANDOM%N+1 instead.
You can:
setopt ksharrays
to enable zero-based indexing.
From man zshoptions:
Emulate ksh array handling as closely as possible. If this
option is set, array elements are numbered from zero, an array
parameter without subscript refers to the first element instead
of the whole array, and braces are required to delimit a sub‐
script (${path[2]}' rather than just$path[2]').