Hye Everyone,
Thanks in advance, Can anyone tell me why cd is not working with var. Is their any other solution to change directory as a variable in Linux in bash scripting
#!/bin/bash
current=$(pm2 l | grep online | grep 7701 | awk ' { print $2 } ')
echo $current
stop=$(pm2 l | grep stopped | grep 7701 | awk ' { print $2 } ')
echo $stop
wrk=$(pm2 show $stop | grep cwd | awk ' { print $5 } ' )
pwd
export wrk="$(pm2 show $stop | grep cwd | awk ' { print $5 } ' )"
echo $wrk
cd $wrk (**Not working** )
Related
I have a long pipeline that I'm constantly reusing in my script, and to make it easy to read I want to put the pipeline in a variable. Is it possible?
cat miami.tmp | grep -A5 "$date" | grep -A3 "$nexthour" | grep "celsius" | grep -E -o '[-]?[0-9].[0-9]' | head -n 1 >> miami.txt
I have tried
temperature=$( | grep -A5 "$date" | grep -A3 "$nexthour" | grep "celsius" | grep -E -
o '[-]?[0-9].[0-9]' | head -n 1 )
or
temperature="| grep -A5 "$date" | grep -A3 "$nexthour" | grep "celsius" | grep -E -o '[-]?[0-9].[0-9]' | head -n 1"
but get errors saying the commands weren't found.
This is a good case for using bash's shell functions. You can define a function like this:
function temperature() { grep foo | grep bar | grep baz; }
just make sure that the last command ends with a semicolon. You call the function with
cat file.tmp | temperature
Functions can also have parameters, accessed with the usual $1, $2 etc. notation, that can be passed in (space-separated) to the function.
$ function hello() { echo "Hello $1!"; }
$ hello world
Hello world!
You should put it in a function.
temperature () {
grep -A5 "$date" |
grep -A3 "$nexthour" |
grep "celsius" |
grep -E -o '[-]?[0-9].[0-9]' |
head -n 1
}
Maybe you want to make the date and the hour into parameters.
temperature () {
grep -A5 "$1" |
grep -A3 "$2" |
grep "celsius" |
grep -E -o '[-]?[0-9].[0-9]' |
head -n 1
}
Separately, this looks like it desperately wants to be refactored to Awk.
temperature () {
awk -v date="$1" nexthour="$2" '
$0 ~ date { p=5 }
p && p-- && ($0 ~ nexthour) { p=3 }
p && p-- && /celsius/ { n = split($0, a, /[^-.0-9]/, a);
for(i=1; i<=n; ++i) if (a[i] ~ /^-?[0-9]\.[0-9]$/)
{ print(a[i]); exit }'
}
(Untested, as you don't supply test data. I had to guess some things. If you are calling it by systematically looping over dates and hours, probably refactor that into the Awk script, too.)
Usage:
temperature 2022-11-24 04 <miami.tmp >>miami.txt
Probably see also https://mywiki.wooledge.org/BashFAQ/050
I need to delete a part of a string via shell script.
E.g: I have this: VERBOSE [61622] [C-0000051f] And I want this:
C-0000051f
Without the brackets in the last portion
How can I do it?
Thank you!
check the awk solution
> data="VERBOSE [61622] [C-0000051f] "
> awk -F"[" ' { print $NF } ' <<< $data | awk -F"]" ' { print $1 } '
C-0000051f
>
EDIT1:
More compact version
> data="VERBOSE [61622] [C-0000051f] "
> awk -F"[[]|]" ' { print $4 } ' <<< $data
C-0000051f
>
As I notice from you answer here, you want to extract some information from a file and then perform a set of actions on that extracted line. The entire line you provided there, could be reduced to the following:
NUMTEL="$1"
IDCALL=$(awk -F "[][]" -v string="$NUMTEL" '/VERBOSE/ && ($0 ~ string){print $(NF-1);exit}' /var/log/asterisk/full)
I was able to do what I needed with the following script.
!/bin/bash
NUMTEL=$1
IDCALL=/usr/bin/grep "VERBOSE" /var/log/asterisk/full | /usr/bin/grep "$NUMTEL" | /usr/bin/head -n1 | /usr/bin/awk '{print $4}' | /usr/bin/cut -d] -f2 | /usr/bin/sed -e 's/\[//g'
echo $IDCALL
Thank you
I'm monitoring from an actively written to file:
My current solution is:
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | grep -q -e "enterpriseID:"
if [ $? = 0 ]
then
((ws_trans++))
fi
echo $LINE | grep -q -e "sc_ID:"
if [ $? = 0 ]
then
((sc_trans++))
fi
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
However when attempting to do this with AWK I don't get the output - the $ws_trans and $sc_trans remains 0
ws_trans=0
sc_trans=0
tail -F /var/log/file.log | \
while read LINE
echo $LINE | awk '/enterpriseID:/ {++ws_trans} END {print | ws_trans}'
echo $LINE | awk '/sc_ID:/ {++sc_trans} END {print | sc_trans}'
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
Attempting to do this to reduce load. I understand that AWK doesn't deal with bash variables, and it can get quite confusing, but the only reference I found is a non tail application of AWK.
How can I assign the AWK Variable to the bash ws_trans and sc_trans? Is there a better solution? (There are other search terms being monitored.)
You need to pass the variables using the option -v, for example:
$ var=0
$ printf %d\\n {1..10} | awk -v awk_var=${var} '{++awk_var} {print awk_var}'
To set the variable "back" you could use declare, for example:
$ declare $(printf %d\\n {1..10} | awk -v awk_var=${var} '{++awk_var} END {print "var=" awk_var}')
$ echo $var
$ 10
Your script could be rewritten like this:
ws_trans=0
sc_trans=0
tail -F /var/log/system.log |
while read LINE
do
declare $(echo $LINE | awk -v ws=${ws_trans} '/enterpriseID:/ {++ws} END {print "ws_trans="ws}')
declare $(echo $LINE | awk -v sc=${sc_trans} '/sc_ID:/ {++sc} END {print "sc_trans="sc}')
printf "\r WSTRANS: $ws_trans \t\t SCTRANS: $sc_trans"
done
I am trying to transform below 2 statements into a shell script
cat all5.log | grep 'Opened\|Closed'> all.log
awk -F ' ' '{print }' all.log | sort | uniq > uniqueFiles.txt
Here is monitorFd.sh bash script
#!/bin/bash
if [ "" != "" ]; then
cat | grep 'Opened\|Closed' > temp.log
awk -F ' ' '{print }' temp.log | sort | uniq > uniqueFiles.txt
while IFS='' read -r line || [[ -n "$line" ]]; do
cmd1=`cat | grep Opened | grep $line | sort | wc -l`
cmd2=`cat | grep Closed | grep $line | sort | wc -l`
echo 'Opened: '$cmd1', Closed: '$cmd2' '$line
done < "uniqueFiles.txt"
rm -f temp.log
else
echo "No target file provided. (hint: trace dump of file-leak-detector.jar)" #syntax error: unexpected end of file
In notepad++ I changed this file to be of UNIX format. Also changed permission to +x, but I am getting below exception.
monitorFd.sh: line 16: syntax error: unexpected end of file
What is wrong with this program?
The Syntax for if in bash is if ... else/elif ... fi. So just add a fi at the end of your file.
I have a shell script which extracts information from Vservers, this is the script:
for i in {130..136}; do
> ./vserver/Info$i
ssh 132.138.180.$i "hostname;
echo 'Virtual'
echo ''
cat /etc/issue | head -1
echo ''
dmidecode | grep Socket | tail -1 | awk '{print \$4}'
echo ''
free -g | grep Mem | awk '{ print \$2 }'
echo ''
fdisk -l | grep Disk | wc -l
echo ''
df -h | grep ^/ | wc -l
echo ''
ifconfig | grep inet | awk '{print \$2 }' | cut -c 6- | awk '\$1=\$1' ORS=' '
echo ''
ifconfig | grep -b1 inet | grep HWaddr | awk '{ print \$5 }' | awk '\$1=\$1' ORS=' '
echo ''
ip route show | grep default | awk '{ print \$3 }' | awk '\$1=\$1' ORS=' '
echo ''
cat /etc/resolv.conf | grep name | awk '{ print \$2 }' | awk '\$1=\$1' ORS=' '
echo ''
mount | grep el01 | awk '{ print \$1 \" -> \" \$3 }' | awk '\$1=\$1' ORS=' '
echo ''
netstat -nr | awk '{ print \$1, \$2, \$3, \$8 }'
echo ''
" >> ./vserver/Info$i
done
I have the following output(example):
el01test
Virtual
Oracle Linux Server
2
7
1
2
19.16.10.111 12.1.0.1 12.1.0.11 12.1.2.11 127.0.0.1
00:22:4F:F9:3C:D8 80:22:05:F7:FE:80:00:00:00:00:00:00:00:00:00:00:00:00:00:00 22:44:22:F4:FE:80:00:00:00:00:00:00:00:00:00:00:00:00:00:00 22:44:22:E2:FE:80:00:00:00:00:00:00:00:00:00:00:00:00:00:00
19.16.10.1
19.16.10.12 19.16.10.15
el01:/export/el011->/home/glassfish/glassfish el01:/export/logs/el01vur01->/home/glassfish/logs el01:/export/home/oem12ag/age->/home/oem12ag/agent
Kernel IP routing
Destination Gateway Genmask Iface
0.0.0.0 192.168.181.1 0.0.0.0 bond0
169.254.0.0 0.0.0.0 255.255.0.0 bond1
17.1.0.0 0.0.0.0 255.255.0.0 bond2
17.1.0.0 0.0.0.0 255.255.0.0 bond1
19.16.0.0 0.0.0.0 255.255.252.0 bond3
19.16.10.0 0.0.0.0 255.255.252.0 bond0
I would like to format my info like this:
hostname OS Core_number Free_memory IP1
IP2
IP3
I've been trying by using awk but I haven't had much luck with it. Thanks for your help!
You can do it with awk, by reading in the lines into an array. Then output the lines containing the information into the required table format.
Here is an example to give you the rough idea:
script.awk
{ info[ i++ ] = $1 }
END { printf("%s\t%s\t%s\t%s\t%s\n", info[0], info[3], info[5], info[7], info[9])
printf("\t\t\t\t\t\t\t%s\n", info[10])
printf("\t\t\t\t\t\t\t%s\n", info[11])
}
Use it like this: awk -f script.awk yourfile.
i finally finish my formatting script, thanks to all of you for your help, finally y had to modify the initial script and then format it using the next code:
for q in $(ls); do awk ' NR >= 19 { route[ j++ ] = $0; next } NF==1 { info[ i++ ] = $1; next } NR == 4 { OS= $0 } NR == 14 { A=split($0, Ip, " "); next } NR == 15 { B=split($0, Mac, " "); next } NR == 17 { C=split($0, Dns, " "); next } NR == 18 { D=split($0, Mount, " "); next } END { if (A > B) max1=A ; else max1=B; if (C > D) max2=C; else max2=D; if (max1 > max2) max=max1; else max=max2; if (max < j) max=j; for (i=0; i<=6; i++) {printf "%s\t ",info[i]} ; for (w = 0; w <= max; w = w+1) { printf("\t\t\t\t\t\t\t%s\t%s\t%s\t%s\t%s\n",Ip[w], Mac[w], Dns[w], Mount[w], route[w]) } } ' $q; done > Fullout
Thanks a lot for your help!
If maybe someone knows how to mix the printf in order to them be able to print at the same line i would appreciated.
I'm also aware that the code could be optimised but i was in a hurry, sorry :(.
I posted one of the output of the initial script in the initial answer if anyone wants to test it for your further scripting.
Thanks
Instead of writing multiple tabs, you could set tabstops using tabs command, or ANSI code inline.
ANSI/VT100 Terminal Control Escape Sequences
or
Linux tabs command
Then, write your output code with a single \t tab.