I'm trying to convert a Debian Bash script into a linux Busybox sh script. I'm stuck trying to convert the following command:
read -r -d '' MESSAGE << EOM
Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
EOM
The problem is with the -d option of read that is not available with Busybox. How can I set a variable ($MESSAGE in this case) to a string with multiple lines that includes values from other variables?
The output MESSAGE is going in a log file and in a message sent by sendmail:
echo "RESULTS: $MESSAGE" >> $LOGFILE
sendmail -S smtp.server.com -f "$FROM" "$RECIPIENTS" <<EOF
subject:$SUBJECT
from:$FROM
$MESSAGE
EOF
Simplest answer is not to use read.
MESSAGE=$(cat <<EOM
Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
EOM
)
MESSAGE=$( printf "%s\n%s\n%s\n%s\n" \
"Return code: $retn_code" \
"Start of backup: $DATESTART" \
"End of backup: $DATEEND" \
"$(df -h | grep '/share/USB')" \
)
You don't need a special command in any shell; just a regular assignment.
message="Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
"
I want to convert this tcpdump output:
IP 10.10.10.1 > 20.20.20.1: ICMP echo request, id 8312, seq 0, length 64
IP 10.10.10.1.17441 > 20.20.20.1.22: Flags [S], seq 3936449810, win 65535, options [mss 1460,nop,wscale 6,sackOK,TS[|tcp]>
to:
IP 10.10.10.1 > 20.20.20.1: ICMP
IP 10.10.10.1.17441 > 20.20.20.1.22: tcp
I tried a lot to covert them with shell script by using the cutcommand but I can't.
Thanks all for your help.
Using awk (or the GNU gawk), setting field separator FS to ":" and assuming dump is inside test.txt:
gawk 'BEGIN{ FS=":" } { if($0 ~ / ICMP /){ print $1 ": ICMP" }else if($0 ~ /tcp[]]>/){ print $1 ": tcp" } }' test.txt
The expected result:
IP 10.10.10.1 > 20.20.20.1: ICMP
IP 10.10.10.1.17441 > 20.20.20.1.22: tcp
tcpdump output could be piped to gawk as
tcpdump <options> | gawk ' ... '
This is strictly a cut method. It is assuming your output will always be of this format. As mentioned, a sed (or awk) version would probably be more dynamic.
The main piece of this is the -d (delimiter) argument and the -f (field) argument. -f can specify a single field or range of fields that are separated by a specified delimiter (I believe tabs are default).
If your output is in a file called output.txt, you can use this little script.
line1="$(head -1 output.txt | cut -d ' ' -f1-5)"
line2="$(tail -1 output.txt | cut -d ' ' -f1-4) $(tail -1 output.txt | cut -d '|' -f2 | cut -d "]" -f1)"
echo "$line1"
echo "$line2"
If your output is stored in a variable called output, you could use this script with your
variable sent as a parameter like ./script.sh "$output"
arg="$1"
line1="$(echo "$arg" | head -1 | cut -d ' ' -f1-5)"
line2="$(echo "$arg" | tail -1 | cut -d ' ' -f1-4) $(echo "$arg" | tail -1 | cut -d '|' -f2 | cut -d "]" -f1)"
echo "$line1"
echo "$line2"
Output:
IP 10.10.10.1 > 20.20.20.1: ICMP
IP 10.10.10.1.17441 > 20.20.20.1.22: tcp
I am working on an existing shell script code which has eval. I feel like that eval is unnecessary here and wanted to remove to avoid Injection.
Could you please check the code and advise why there is an eval in the code.
FILE_PATH=`echo $1 | awk '{ print $10 }' | cut -f2 -d'"'
FILE_PATH=`(eval "echo ${FILE_PATH}")`
if $1 is something like that ---"~/tttttttt.txt.
FILE_PATH will be ~/tttttttt.txt without eval.
but with eval;
FILE_PATH will be /home/user/tttttttt.txt
#!/bin/bash
path='-----"~/tttttttt.txt'
FILE_PATH=`echo $path | awk '{ print $1 }' | cut -f2 -d'"'`
echo "${FILE_PATH}"
ls -lart ${FILE_PATH}
FILE_PATH=`(eval "echo ${FILE_PATH}")`
echo $FILE_PATH
ls -lart ${FILE_PATH}
if run above script, output:
~/tttttttt.txt
ls: cannot access ~/tttttttt.txt: No such file or directory
/home/user/tttttttt.txt
-rw-rw-r-- 1 user user 0 Aug 26 15:54 /home/user/tttttttt.txt
Hello i have some csv files like that:
"N.º","Fecha Tiempo, GMT-03:00","Temp, °C (LGR S/N: 10466185, SEN S/N: 10466185, LBL: Temperatura)","Acoplador separado (LGR S/N: 10466185)","Acoplador adjunto (LGR S/N: 10466185)","Host conectado (LGR S/N: 10466185)","Parado (LGR S/N: 10466185)","Final de archivo (LGR S/N: 10466185)"
1,03/03/14 01:00:00 PM,25.477,Registrado,,,,
2,03/03/14 02:00:00 PM,24.508,,,,,
3,03/03/14 03:00:00 PM,26.891,,,,,
4,03/03/14 04:00:00 PM,25.525,,,,,
5,03/03/14 05:00:00 PM,27.358,,,,,
Then i wanna convert the second field of data-hour in two fields: date, hour
I'm ok with split date and hour, but when i try to convert hours in am-pm to hours in 24hrs i failed.
Using for all files this command:
awk -F"," '{print $2}' *.csv|awk '{print $1","$2" "$3}'
I'm arriving to that command, in particular:
echo "11:04:44 PM" | awk -F, -v hora=$1 '{system("date --date=$hora +%T");print $hora}'
00:00:00
11:04:44 PM
The problem is the variable inside system(date... beacuse it returns 0 or empty.
Then the question is about how to do thath.
And finnally how to insert tath changes inside the file.
Thanks, very thanks!
On my machine (Mac OS), the command you need is
echo "11:22:33 AM" | awk '{split($1,a,":"); if($2=="PM") {a[1]=a[1]+12;} print a[1] ":" a[2] ":" a[3]}'
This does the splitting of the time manually (rather than relying on date which is a bit platform dependent) and adds 12 to the time if it's PM.
So the whole thing becomes:
awk -F"," '{print $2}' *.csv | awk '{split($1,a,":"); if($2=="PM") {a[1]=a[1]+12;} print a[1] ":" a[2] ":" a[3]}'
Although you really want to skip the first line in the file, so
awk -F"," 'if(NR>1){print $2}' *.csv | awk '{split($1,a,":"); if($2=="PM") {a[1]=a[1]+12;} print a[1] ":" a[2] ":" a[3]}'
Thanks!
Now, after many hours i can convert the time using 'date', then the code is:
echo "11:04:44 PM" | awk -F, -v hora=$1 '{system("date --date=\""$hora"\" +%T");print $hora}'
With thaat you can compare the time in 24hrs and AM/PM
The details was the '\"' before and after the '$hora' variable
;)
Then, to converte the complete DAte-Hour from the csv file you have to put:
awk -F"," '{if (FNR>=3) print $2}' *.csv | awk '{print $1","$2" "$3}'| awk -F, '{system("printf "$1", & date --date=\""$2"\" +%T")}'
Now, i have to design a new file qith the id and values columns....
I have a string ABCD20110420.txt and I want to extract the date out of it. Expected 2011-04-20
I can use replace to remove the text part, but how do I insert the "-" ?
# echo "ABCD20110420.txt" | replace 'ABCD' '' | replace '.txt' ''
20110420
echo "ABCD20110420.txt" | sed -e 's/ABCD//' -e 's/.txt//' -e 's/\(....\)\(..\)\(..\)/\1-\2-\3/'
Read: sed FAQ
Just use the shell (bash)
$> file=ABCD20110420.txt
$> echo "${file//[^0-9]/}"
20110420
$> file="${file//[^0-9]/}"
$> echo $file
20110420
$> echo ${file:0:4}-${file:4:2}-${file:6:2}
2011-04-20
The above is applicable to files like your sample. If you have files like A1BCD20110420.txt, then will not work.
For that case,
$> file=A1BCD20110420.txt
$> echo ${file%.*} #get rid of .txt
A1BCD20110420
$> file=${file%.*}
$> echo "2011${file#*2011}"
20110420
Or you can use regular expression (Bash 3.2+)
$> file=ABCD20110420.txt
$> [[ $file =~ ^.*(2011)([0-9][0-9])([0-9][0-9])\.*$ ]]
$> echo ${BASH_REMATCH[1]}
2011
$> echo ${BASH_REMATCH[2]}
04
$> echo ${BASH_REMATCH[3]}
20
echo "ABCD20110420.txt" | sed -r 's/.+([0-9]{4})([0-9]{2})([0-9]{2}).+/\1-\2-\3/'
$ file=ABCD20110420.txt
$ echo "$file" | sed -e 's/^[A-Za-z]*\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)\.txt$/\1-\2-\3/'
This only requires a single call to sed.
echo "ABCD20110420.txt" | sed -r 's/.{4}(.{4})(.{2})(.{2}).txt/\1-\2-\3/'