echo -n not working with secondary operation - echo

I have a few filenames in a directory
blabla.01
blabla.02
...
I'm trying to make a new file with the following format:
01 new stuff here
02 more new stuff
...
I wrote a script and dumbed it down a bit:
#!/bin/bash
FILES=$(find . -type f -name "blabla*" | awk -F'[.]' '$(NF-1)>=1' | sort)
for f in $FILES
do
echo -n $f | cut -d "." -f 3
echo "test"
done
the 'test' will be the output of another code..
However in this example i get something like:
01
test02
test
Thanks

Try
printf '%s%s\n' "$(echo "$f" | cut -d . -f3)" "test"
The echo | cut could probably be replaced with something like "${f##*.}" if you always want the laat field.

Related

How to store hexdump of a string in hex format in powershell?

I am trying to convert a shell script to PowerShell script, I am facing issue with below shell commands
label=`echo -n "signing key" | hexdump -ve '/1 "%02x"'`
sign_nist="00000001${label}0000000100"
/testing$ echo $sign_nist
000000017369676e696e67206b65790000000100
/testing$ echo -n $sign_nist | sed -e 's/../\\x&/g'
\x00\x00\x00\x01\x73\x69\x67\x6e\x69\x6e\x67\x20\x6b\x65\x79\x00\x00\x00\x01\x00
/testing$ echo -ne "$(echo -n $sign_nist | sed -e 's/../\\x&/g')"
signing key
when I try to do the same thing in PowerShell using the below commands my output is different
PS C:\testing> $sign_nist
000000017369676E696E67206B65790000000100
PS C:\testing>
$prep = $sign_nist -split '(..)' -ne ''
for($i = 0; $i -lt $prep.length; $i++)
{
$prep[$i] = [System.Convert]::ToUInt32($prep[$i],16)
}
$sign_key_input=$prep -join '';
$sign_key_input
00011151051031101051101033210710112100010
could someone please help me how to get signing key to $sign_key_input with PowerShell?
Thanks in advance for the help.
I think appending \x for each byte works for linux to treat it as hex value but how to indicate the same in powershell?

The reason why the script written in centos does not work in crontab

I registered the script to crontab to run every 10 minutes. When I run the script, the log is printed, but if I put it on the cron tab and wait, the log does not appear, so it seems that it does not work. If you have anything I need to add or edit, please let me know :(
my sh script
#!/bin/sh
pslist=`ps -ef | grep ffprobe | awk '{print $2}'`
pscount=`ps -ef | grep ffprobe | wc -l`
logs='/apps/kMobile/kEncoderPy/batch/kill_ffprobe.log'
timestamp=`date +%Y%m%d--%H:%M`
echo "[$timestamp] Kill the Batch process Start :: "$timestamp >> $logs
echo "[$timestamp] process ffprobe running count :: "$pscount >> $logs
for pid in $pslist
do
rtime=`ps -p $pid -o etime | tail -1`
rtime=$rtime | tr -d ' '
minutes=${rtime:6:2}
if [$rtime != "ELAPSED" ]; then
if [ $minutes -gt 10 ]; then
`kill -9 $pid`
echo "[$timestamp] passed 10 minute kill process id : "$pid >> $logs
echo "[$timestamp] process kill after ffprobe running count : "$psount >> $logs
fi
fi
done
my crontab -e
*/10 * * * * /apps/kMobile/batch/kill_ffprobe.sh

Converting a shell script to PowerShell

After switching an application from Linux to Windows, I need to convert a shell script to a Windows equivalent. My choices were basically batch and PowerShell and I decided to give a shot to PowerShell.
For anyone interested, it's a local check for Check_MK to get information about SoftEther installed version and the number of sessions with performance data.
The initial shell script was as follow:
#!/bin/sh
cmd=$(/usr/local/vpnserver/vpncmd localhost:port /server /password:password /in:/usr/lib/check_mk_agent/local/vpncmd.txt)
version=$(echo "$cmd" | head -4 | tail -1)
sessions=$(echo "$cmd" | grep Sessions | awk '$1=$1' | cut -c21-22)
if [ -z "$version" ]; then
echo "3 VPN_Version - Can't get the information from vpncmd"
else
echo "0 VPN_Version - SoftEther VPN Server $version"
fi
if [ -z "$sessions" ]; then
echo "3 VPN_Sessions - Can't get the information from vpncmd"
else
echo "P VPN_Sessions sessions=$sessions;2;2"
fi
I basically got everything working except the 2 hardest lines of code:
cd "C:\Program Files\SoftEther VPN Server"
$cmd = vpncmd localhost:port /server /password:password /in:vpncmd.txt
$version=
$sessions=
if($version -eq $null) {
echo "3 VPN_Version - Can't get the information from vpncmd"
} else {
echo "0 VPN_Version - SoftEther VPN Server $version"
}
if($sessions -eq $null) {
echo "3 VPN_Sessions - Can't get the information from vpncmd"
} else {
echo "P VPN_Sessions sessions=$sessions;2;2"
}
I need help with going from the head, tail, grep, awk and cut one liners to whatever is equivalent in PowerShell. I read about Get-Content but I'm not sure if it's the most efficient way to do this and would like to prevent going from 1 line definition to 10 lines if that's possible to be as efficient in PowerShell.
Sample output of vpncmd's output: https://pastebin.com/J5FcHzHK
with the data being an array of lines & the word Version appearing multiple times in the actual source, the code needs to change a tad. in this version, it uses the way that -match works on an array to give the whole line as a result. that requires working on the output line to parse the desired data.
$Version = ($Vpncmd_Output -match '^Version \d{1,}\.\d{1,}' -split 'Version ' )[-1].Trim()
$SessionCount = [int]($Vpncmd_Output -match 'Number of Sessions\s+\|').Split('|')[-1].Trim()
$Version
$SessionCount
output ...
4.29 Build 9680 (English)
0
using the data in your PasteBin post, and presuming that is a multiline string, not an array of strings, this seems to work [grin] ...
$Vpncmd_Output -match '(?m)Number of Sessions\s+\|(?<Sessions>.*)'
$Matches.Sessions
# output = 0
$Vpncmd_Output -match '(?m)Version (?<Version>.+)'
$Matches.Version
# output = 4.29 Build 9680 (English)
i tried to combine the regex into one, but failed. [blush] the way i have it requires two passes, but it does work.

How to find files based on other files in current directory, with find command?

Want to find all mkv files without having same-name ass/srt file in the same folder.
How can I do that?
for example, I have following directory:
folder_1
|----folder_2
| |-----a.mkv
| |-----a.srt
|----folder_3
| |-----b.mkv
|----folder_4
|-----c.mkv
|-----c.ass
The search result should be: folder_1/folder_3/b.mkv.
Many Thanks.
Get answer from my friends, share it:
find . -name "*.mkv" -o -name "*.ass" -o -name "*.srt"| sort |rev|uniq -s 3 -u| rev|rgrep ".mkv"
BTW, if you are using synology nas, which does not have 'rev' command, you can walkaround it by using a python script(rev.py):
import sys
if __name__ == '__main__':
if len(sys.argv) >= 2:
for arg in sys.argv[1:]:
print '"' + arg[::-1] + '"'
and the script will be changed to:
find . -name "*.mkv" -o -name "*.ass" -o -name "*.srt"| sort |awk '{print "\"", $0,"\""}' OFS=""|xargs python rev.py |uniq -s 3 -u| xargs python rev.py | grep ".mkv\""

Format string inplace in PowerShell

I've notice I'm not manage to format string in-place when calling a function in PowerShell. I need to format it because I want to output a number as hex and not decimal.
This didn't work like so:
WriteLog "Running Step | Retry=$RetryCount | EnabledDevices={0:X}" -f $EnabledDevices
It only works if I store the result in variable and then use it like so:
$Log = "Running Step | Retry=$RetryCount | EnabledDevices={0:X}" -f $EnabledDevices
WriteLog $Log
If there a way to do it in one statement instead of two?
Just put it in parentheses:
WriteLog ("Running Step | Retry=$RetryCount | EnabledDevices={0:X}" -f $EnabledDevices)