I have function
function contain() {
if [[ $(echo "$1" | grep "$2" | wc -c) -gt 0 ]]; then
return 0
else
return 1
fi
}
when I write instead
if [[ $(md5sum -c tool | grep -v "OK$" | wc -c) -gt 0 ]]; then
this
if contain "$(md5sum -c tool)" "OK$"
wants not work
Related
So I have this piece of code:
if type -P sensors 2>/dev/null; then
returnString=`sensors`
#amd
if [[ "${returnString/"k10"}" != "${returnString}" ]] ; then
$SENSORS | grep Tdie | $CUT -d ' ' -f 10 | { echo "{"; cat; echo "}"; } | tr -d '\n'
#intel
elif [[ "${returnString/"core"}" != "${returnString}" ]] ; then
fromcore=${returnString##*"coretemp"}
$ECHO ${fromcore##*Physical} | $CUT -d ' ' -f 3 | $CUT -c 2-5 | _parseAndPrint
fi
else
$ECHO "[]" | _parseAndPrint
fi
Its output is:
/usr/bin/sensors
{+31.9°C}
But the desired output is:
{+31.9°C}
Sensors output:
/usr/bin/sensors
nvme-pci-0100
Adapter: PCI adapter
Composite: +33.9°C (low = -0.1°C, high = +74.8°C)
(crit = +79.8°C)
k10temp-pci-00c3
Adapter: PCI adapter
Vcore: 1.12 V
Vsoc: 888.00 mV
Tctl: +31.1°C
Tdie: +31.1°C
Icore: 8.00 A
Isoc: 5.00 A
I would really prefer to leave if type -P sensors 2>/dev/null; then in place, so it can still detect if it is Intel or AMD.
The line is run in a script file, which can be found here.
The problem is the type command. You only need the exit status of this command, but it writes in your case the path of the sensor command to stdout. You should redirect its stdout to /dev/null.
I am just not the smartest right now, should've looked at the rest of the Code and should sleep xD. I could just remove the check,that checks if "sensors" is accessible (at least i think that is what it was used for).
so from
if type -P sensors 2>/dev/null; then
returnString=`sensors`
#amd
if [[ "${returnString/"k10"}" != "${returnString}" ]] ; then
sensors | grep Tdie | $CUT -d ' ' -f 10 | { echo "{"; cat; echo "}"; } | tr -d '\n'
#intel
elif [[ "${returnString/"core"}" != "${returnString}" ]] ; then
fromcore=${returnString##*"coretemp"}
$ECHO ${fromcore##*Physical} | $CUT -d ' ' -f 3 | $CUT -c 2-5 | _parseAndPrint
fi
else
$ECHO "[]" | _parseAndPrint
fi
I made
returnString=`sensors`
#amd
if [[ "${returnString/"k10"}" != "${returnString}" ]] ; then
sensors | sed -nE 's/^Tdie: *([^ ]*)/{\1}/p'
#intel
elif [[ "${returnString/"core"}" != "${returnString}" ]] ; then
fromcore=${returnString##*"coretemp"}
$ECHO ${fromcore##*Physical} | $CUT -d ' ' -f 3 | $CUT -c 2-5 | _parseAndPrint
fi
I am using bash scripting to check whether two specific directories are containing some specific number of files. lets assume 20. Is it possible to check it in a line inside a if statement?
#!/bin/bash
if [ [ls /path/to/dir1 | wc -l ] != 20 || [ ls /path/to/dir2 | wc -l ] != 50 ]; then
echo "do this!"
elif
echo "do that!"
fi
The syntax is incorrect:
if [[ $(ls /path/to/dir1 | wc -l) != 20 || $(ls /path/to/dir2 | wc -l) != 50 ]]
then
echo "do this!"
else
echo "do that!"
fi
(I move the position of the then for readability)
With two square brackets [[ you can use || for "or" instead of -o, which is closer to conventional languages. Strictly speaking the [[ does pattern matching, so although the code above will work, an arithmetic test should really use ((:
if (( $(ls /path/to/dir1 | wc -l) != 20 || $(ls /path/to/dir2 | wc -l) != 50 ))
then
echo "do this!"
else
echo "do that!"
fi
The $( ) runs a subshell and captures the output.
Correct if syntax:
if [ $(ls /path/to/dir1 | wc -l) -ne 20 -o $(ls /path/to/dir2 | wc -l) -ne 50 ]
But in your example you shouldn't use just elif. Use either else, or specify condition for elif, exmpl:
if [ $(ls /path/to/dir1 | wc -l) -ne 20 -o $(ls /path/to/dir2 | wc -l) -ne 50 ]
then
echo "Do smth"
elif [ $(ls /path/to/dir3 | wc -l) -ne 100 ]
then
echo "Do anything else"
fi
#!/bin/bash
a=2
b=2
COUNTER=0
sam="abcd"
sam1="xyz"
sam2="mno"
for x in ls | grep .rpm
do
`C=rpm -qpR $x | grep -v CompressedFileNames | grep -v PayloadFilesHavePrefix | wc -l`
if [ "sam2"!="$sam1" ]
then
echo "${sam1}"
echo "${sam2}"
if [ $C -eq $a ]
then
COUNTER=$((COUNTER+1))
echo "${x}"
eval sam=$x
#eval sam1=sam | cut -d '-' -f 1
sam1=`echo "${sam}"| cut -d '-' -f 1`
if [ $COUNTER -eq $b ]
then
break
fi
fi
fi
sam2=`echo "${x}"| cut -d '-' -f 1`
done
This is the output I am getting:
xyz
mno
comps-4ES-0.20050107.x86_64.rpm
comps
comps
comps-4ES-0.20050525.x86_64.rpm
My question is: why is the if condition returning true despite sam1 and sam2 being equal? I have checked for non-equality.
Response is the same even if I use
if [ $C -eq $a ] && [ "$sam2" != " $sam1" ]
As Ansgar Wiechers pointed out, you're missing a "$" in front of the sam2 variable. That way, you're comparing the literal string "sam2" with the string value of $sam1 (which initially is set to "xyz"). What you want to do is compare the string values of both variables:
if [ "$sam2" != "$sam1" ]
Regarding $C, you should only include the commands to be evaluated inside backticks, not the evaluation itself. This is called a command substitution - a subshell is created in which the commands are executed, and the backtick expression is substituted by the computed value. The line should look like this:
C=`rpm -qpR $x | grep -v CompressedFileNames | grep -v PayloadFilesHavePrefix | wc -l`
Your for loop also needs a command substitution: for x in ls | grep .rpm makes it look as if you're piping the output of a for command into grep. What you want to do is iterate over the ls | grep part, which you can do with the following command substitution:
for x in `ls | grep .rpm`
Hi Guys Got the solution:
#!/bin/bash
read -p "enter dep number" a
read -p "enter no of rpms" b
COUNTER=0
sam="abcd"
sam1="xyz"
sam2="mno"
for x in `ls | grep .rpm`
do
C=`rpm -qpR $x |grep -v CompressedFileNames | grep -v PayloadFilesHavePrefix | wc -l`
# echo "${C}:c"
if [ $C -eq $a ] && [ "$sam2" != "$sam1" ]
then
COUNTER=$((COUNTER+1))
# echo "${COUNTER}:counter"
# echo "${x}"
eval sam=$x
#eval sam1=sam | cut -d '-' -f 1
sam1=`echo "${sam}"| cut -d '-' -f 1`
if [ $COUNTER -eq $b ]
then
break
fi
fi
sam2=`echo "${x}"| cut -d '-' -f 1`
#echo "${sam2}"
#echo "${sam1}"
done
ls>out
count="$(ls|wc -l)"
for i in {1..$count-2}
do
if[ "$(cat out|head -$count | tail -1)" != "rename.sh" ]; then
mv "$1$count$2" | cat out | head -$count | tail -1
fi
done
I really don't get why is not working...
ls>out
count="$(ls|wc -l)"
for i in {1..$count-2}
do
if [ "$(cat out|head -$count | tail -1)" != "rename.sh" ]; then
mv "$1$count$2" | cat out | head -$count | tail -1
fi
done
put a space between if and '[' , the script will be work.
Here's my script:
while [[ $startTime -le $endTime ]]
do
thisfile=$(find * -type f | xargs grep -l $startDate | xargs grep -l $startTime)
fordestination=`cut -d$ -f2 $thisfile | xargs cut -d ~ -f4`
echo $fordestination
startTime=$(( $startTime + 1 ))
done
I think your cut and grep commands could get stuck. You probably should make sure that their parameters aren't empty, by using the [ -n "$string" ] command to see if $string isn't empty. In your case, if it were empty, it wouldn't add any files to the command that would use it afterwards, meaning that the command would probably wait for input from the command line (ex: if $string is empty and you do grep regex $string, grep wouldn't receive input files from $string and would instead wait for input from the command line). Here's a "complex" version that tries to show where things could go wrong:
while [[ $startTime -le $endTime ]]
do
thisfile=$(find * -type f)
if [ -n "$thisfile" ]; then
thisfile=$(grep -l $startDate $thisfile)
if [ -n "$thisfile" ]; then
thisfile=$(grep -l $startTime $thisfile)
if [ -n "$thisfile" ]; then
thisfile=`cut -d$ -f2 $thisfile`
if [ -n "$thisfile" ]; then
forDestination=`cut -d ~ -f4 $thisfile`
echo $fordestination
fi
fi
fi
fi
startTime=$(( $startTime + 1 ))
done
And here's a simpler version:
while [[ $startTime -le $endTime ]]
do
thisfile=$(grep -Rl $startDate *)
[ -n "$thisfile" ] && thisfile=$(grep -l $startTime $thisfile)
[ -n "$thisfile" ] && thisfile=`cut -d$ -f2 $thisfile`
[ -n "$thisfile" ] && cut -d ~ -f4 $thisfile
startTime=$(( $startTime + 1 ))
done
The "-R" tells grep to search files recursively, and the && tells bash to only execute the command that follows it if the command before it succeeded, and the command before the && is the test command (used in ifs).
Hope this helps =)