I'm able to run below code on Linux to get unmounted disk succesfully.
alldisks=`/sbin/sfdisk -s | grep -v 'total' | awk -F':' '{print $1}'`
unmounted_disks=""
for disk in $alldisks
do
/sbin/blkid -o list | grep -q "$disk"
if [ $? -ne 0 ]; then
unmounted_disks="$unmounted_disks $disk"
fi
done
echo $unmounted_disks
But I'm automating this through workbook using PowerShell. I tried below way of sending to Linux after connecting using New-SSHSession:
**$script** = ('alldisks=`/sbin/sfdisk -s | grep -v ''total'' | awk -F '':'' ''{print $1}''`
unmounted_disks=""
echo $unmounted_disks
for disk in $alldisks
do /sbin/blkid -o list | grep -q ''$disk''
`n
if [ $? -ne 0 ]
then unmounted_disks=''$unmounted_disks $disk''
fi
done
echo $unmounted_disks')
$output = **Invoke-SSHCommand** -Index $session.SessionId **-Command $script** -TimeOut 900 -ErrorAction Stop
I get below error:
bash: -c: line 8: syntax error near unexpected token `fi'
bash: -c: line 8: ` fi '
Use a here-string for your script:
$script = #'
alldisks=`/sbin/sfdisk -s | grep -v 'total' | awk -F ':' '{print $1}'`
unmounted_disks=""
echo $unmounted_disks
for disk in $alldisks
do
/sbin/blkid -o list | grep -q "$disk"
if [ $? -ne 0 ]; then
unmounted_disks="$unmounted_disks $disk"
fi
done
echo $unmounted_disks
'#
Related
I have two praised xml data as following 2 lines:
<address type='pci' domain='0x0000' bus='0x04' slot='0x10' function='0x0 '/>
<address type='pci' domain='0x0000' bus='0x82' slot='0x10' function='0x1 '/>
I want to combine the value of domain,bus, slot and function of each line with ":" as below result:
$line1=0000:04:10:0
$line2=0000:82:10:1
above PCI BDF format (0000:00:00:0) is for next checking.
thanks,
finally, I added partial code into a intel example script for sr-iov host VFs management, it works for me.
the output result looks like below:
here is the script:
#!/bin/bash
DOMAINS=$(virsh list --all | grep "running" | awk '{print $2}')
vm_list="VM_Name VF_BDF"
for DOMAIN in $DOMAINS; do
temp=("$(virsh dumpxml $DOMAIN | grep -w hostdev -A9 |sed -n '/source>/,/<\/source>/p' |grep "<address" | sed "s/.*domain='0x\([^']\+\)'.*bus='0x\([^']\+\)'.*slot='0x\([^']\+\)'.*function='0x\([^']\+\)'.*/\1:\2:\3.\4/")")
vm_list+="\\n${DOMAIN} ${temp}"
done
#echo -e $vm_list
NIC_DIR="/sys/class/net"
for i in $( ls $NIC_DIR) ;
do
if [ -d "${NIC_DIR}/$i/device" -a ! -L "${NIC_DIR}/$i/device/physfn" ]; then
declare -a VF_PCI_BDF
declare -a VF_INTERFACE
k=0
for j in $( ls "${NIC_DIR}/$i/device" ) ;
do
if [[ "$j" == "virtfn"* ]]; then
VF_PCI=$( readlink "${NIC_DIR}/$i/device/$j" | cut -d '/' -f2 )
VF_PCI_BDF[$k]=$VF_PCI
#get the interface name for the VF at this PCI Address
for iface in $( ls $NIC_DIR );
do
link_dir=$( readlink ${NIC_DIR}/$iface )
if [[ "$link_dir" == *"$VF_PCI"* ]]; then
VF_INTERFACE[$k]=$iface
fi
done
((k++))
fi
done
NUM_VFs=${#VF_PCI_BDF[#]}
if [[ $NUM_VFs -gt 0 ]]; then
#get the PF Device Description
PF_PCI=$( readlink "${NIC_DIR}/$i/device" | cut -d '/' -f4 )
PF_VENDOR=$( lspci -vmmks $PF_PCI | grep ^Vendor | cut -f2)
PF_NAME=$( lspci -vmmks $PF_PCI | grep ^Device | cut -f2).
echo "Virtual Functions on $PF_VENDOR $PF_NAME ($i):"
echo -e "PCI BDF\t\tInterface\tVM_Name"
echo -e "=======\t\t=========\t==============="
for (( l = 0; l < $NUM_VFs; l++ )) ;
do
guestVM=$(echo -e $vm_list | grep ${VF_PCI_BDF[$l]} | awk '{print $1}')
if [ $? -ne 0 ]; then
echo "Fail to find the VF_BDF with guestVM. "
exit
fi
if [[ $guestVM == "" ]]; then
echo -e "${VF_PCI_BDF[$l]}\t${VF_INTERFACE[$l]}\t\t${guestVM}"
else
echo -e "${VF_PCI_BDF[$l]}\t" used "\t\t${guestVM}"
fi
done
unset VF_PCI_BDF
unset VF_INTERFACE
echo " "
fi
fi
done
Try this:
cat input | sed "s/.*domain='0x\([^']\+\)'.*bus='0x\([^']\+\)'.*slot='0x\([^']\+\)'.*function='0x\([^']\+\)'.*/\0=\1:\2:\3:\4/"
Where input is the file with the data
i am using WSL (Ubuntu) in Windows. i used bash script.sh for the script below:
#! /bin/sh
#################LOAD FILES###################
lead_SNPs=`grep "lead_SNPs" ../prep/files.txt | cut -f2`
bfile=`grep -w "bfile" ../prep/files.txt | cut -f2`
bfile_list=`grep -w "bfile_list" ../prep/files.txt | cut -f2`
r2=`grep "r2" ../prep/parameters.txt | cut -f2`
###############LD###########################
if [ ${bfile} = "NA" ]; then
cat ${bfile_list} | while read line; do
file=${line}
file_n=`echo $file |awk -F '/' '{print $NF}'`
echo 'Calculating LD'
plink --bfile ${file} --r2 --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}
done
else
file=${bfile}
file_n=`echo $file |awk -F '/' '{print $NF}'`
echo ${file_n}
plink --bfile ${file} --r2 --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}
fi
but i get this error
Syntax error near unexpected token `fi`
can you correct my code please? i can not understand where i made mistake.
Try to change:
if [ ${bfile} = "NA" ]; then
to
if [ "${bfile}" = "NA" ]; then
I suspect ${bfile} is empty which expanded to:
if [ = "NA" ]; then
in your original line.
The first line in your script doesn't look right.
It should be
#!/bin/sh
or if using bash shell:
#!/bin/bash
I currently have a script that checks the MAC address, tt looks like this
"m"|"-m" )
if [ ! -n "$2" ]
then
echo "Enter MAC address"
exit 0
fi
out1=`cat $dhcp_files/* | grep -i "$2" 2>/dev/null`
ip=`cat $dhcp_files/* | grep -i "$2" | awk '{print $8}' | sed s/\;//g 2>/dev/null`
out=` grep -i $ip $shapy/* | awk '{print $8}' 2>/dev/null`
echo -en ''$out1' on '$out' \n'
this is done like that: ./mac_check -m 00:00:00:00:00:00
The result of this script:
host Jon_Rosewelt_6_5 { hardware ethernet 00:1d:4e:b5:d4:10; fixed-address 192.168.101.19; } on eth4
but I would like to do to the script received any form of mac address
aka. ./check_mac -m 001d.4eb5.d410 **or** 001d4eb5d410 **or** 00-1d-4e-b5-d4-10
How can I do that?
With GNU sed and Solaris sed:
echo 001d.4eb5.d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 001d4eb5d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00-1d-4e-b5-d4-10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00:1d:4e:b5:d4:10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
Output:
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
With your code:
"m"|"-m" )
mac="$2" # added
if [ ! -n "$mac" ] # changed
then
echo "Enter MAC address"
exit 0
fi
mac="$(echo "$mac" | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//')" # added
out1=`cat $dhcp_files/* | grep -i "$mac" 2>/dev/null` # changed
ip=`cat $dhcp_files/* | grep -i "$mac" | awk '{print $8}' | sed s/\;//g 2>/dev/null` # changed
out=` grep -i $ip $shapy/* | awk '{print $8}' 2>/dev/null`
echo -en ''$out1' on '$out' \n'
I'm at the following directory...
/var/log/homes
and I would like a script to check for any lines starts with 'Error' and contains a word 'NotAuthorized' and not contains '13024' or '31071'.
Within /var/log/homes/, there are 700 files, but that doesn't matter...
Below is what I have...
#!/bin/bash
host=`hostname`
date=`date`
monitor='Error'
pattern='Error'
pattern2='NotAuthorized'
ignore=13024
ignore2=31071
logfile='/var/log/homes/*'
mailto='test#linux.com'
if [ -e $logfile ]
then
if [ `grep -i "$pattern" "$pattern2" "$logfile" | grep -v $ignore $ignore ]
then
echo "Errors found in $monitor at $date - see log $logfile on server $host for full details" |mail -s "ALERT - $errors in logs, please review" $mailto
elif [ `grep -i "$pattern2" "$logfile" |wc -l` -lt 1 ]
fi
else
echo "Logfile $logfile doesn't exist on server $host at $date, this is probably bad, please investigate" |mail -s "ALERT - $monitor monitor has an issue" $mailto
fi
This should work:
grep "^Error" $logfile | grep "NotAuthorized" | grep -v "13024" | grep -v "31071"
grep "^Error": get the lines starting ith "Error"
grep "NotAuthorized": get the lines containing "NotAuthorized"
grep -v "XXX": get the lines not containing "XXX"
It looks fine to me. Just cross check if you are using all of these conditions:
grep "^Error" $file | grep "NotAuthorized" | grep -Ev "31071|13024"
^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
lines starting with Error | |
containing NotAuthorized
exclude either lines containing 31071 or 13024
To know which file is it happening in, use -H option in grep:
grep -H "^Error" $file | grep "NotAuthorized" | grep -Ev "31071|13024"
So all together you can do:
grep -H "^Error" /var/log/homes/* | grep "NotAuthorized" | grep -Ev "31071|13024"
Print any lines that start with 'Error' and contain the word 'NotAuthorized' and does not contain '13024' or '31071':
sed -n '/^Error/{/NotAuthorized/{/13024/!{/31071/!p}}}'
This awk may do:
awk '/^Error/ && /NotAuthorized/ && !/31071|13024/' $logfile
I have the following command:
egrep -e "\\\\" pom.xml | egrep -v -e "\\\\$"
I want to assign it to a variable so that I can echo it later in this code:
egrep -e "\\\\" pom.xml | egrep -v -e "\\\\$" &> /dev/null
if [ $? == 0 ]; then
echo "Errors in POM were found"
echo "line matches (result of the command)"
fi
POM_ERRORS=$(egrep -e "\\\\" pom.xml | egrep -v -e "\\\\$")