I have this script:
#!/bin/bash
echo Id,Name,Amount,TS > lfs.csv
I want to insert values that will match the columns I created (as above in the script) , I want for example to insert: 56,"Danny",579,311413567
I want to be able to insert it using 'FOR' loop which will insert values without stopping but to change the values for each insert
More detail would be useful what you like to achieve exactly, so I made a infinite for loop which put line into the csv incremented numbers you provide by $i. ( I cannot make comment yet to ask you )
Update:
I still using a infinite loop to get a number counting up to the endless, and using a variable (u_id) to count from 1 to 100 then reset it back to 1 if it is reach 100.
#!/bin/bash
echo 'Id,Name,Amount,TS,unique_ID' > lfs.csv
u_id=0
for (( id=1 ; ;id++ ))
do
[[ $u_id == 100 ]] && (( u_id = 1 )) || (( u_id +=1 ))
echo $id",Danny_"$id","$id","$id","$u_id >> lfs.csv
done
If you like to start Amount and TS from bigger number you can do that by modifing $id to $(( id + 50000 )) like:
echo $id",Danny_"$id","$(( id + 300 ))","$(( id + 50000 ))","$u_id >> lfs.csv
Related
I want to to check every 3 second if the difference of the numbers is higher than 1000. How can I get the old value, for example: 16598, every time?
while true; do
testbro=$(wc -l < /home/web/log/access.log)
echo $testbro
sleep 3
done
It outputs as wanted:
16414
16471
16533
16598
16666
If your intention is to detect when your file grows >= 1000 lines in a 3 second period, you could do this:
#!/bin/bash
last_size=$(wc -l < /home/web/log/access.log)
while true; do
sleep 3
curr_size=$(wc -l < /home/web/log/access.log)
if ((curr_size - last_size >= 1000)); then
echo "$curr_size"
fi
last_size=$curr_size
done
I have a scripts that needs to change the number each time is run. There can be max 3 digits from 0 to 999, it needs to repeat after it reaches 999.
#!/bin/sh
#Ncat is a part of the nmap, install nmap to use ncat
#Define variables
i=poland.aprs2.net
port=14580
lat=3439.94N
lon=02517.67E_ #_ is a symbol for WX station
user=APRS
password=99999
#Generate authentication data
aprsauth="user $user pass $password filter m/50"
#Generate Weather data
xastir="$user>APX206,TCPIP*:!$lat/$lon".../...g...t...h41X210""
#Telemetry data
t1="$user>APX206,TCPIP*:T#672,060,000,000,000,000,00000000"
#Send data to the APRS server
printf "%s\n" "$aprsauth" "$xastir" "$t1"| ncat --send-only $i $port
#Output control
printf "%s\n" "$aprsauth" "$xastir" "$t1"
Code that needs to be changed is T#672
from:
t1="$user>APX206,TCPIP*:T#672,060,000,000,000,000,00000000"
T#xxx is a sequence number in APRS protocol, and it only supports 3 digits. Each telemetry report needs a different sequence number.
You need to maintain some sort of external state: write the last number used to a file, and read from that file on startup so you can incremented t it.
read num < /var/run/myscript/last_value
num=$((num + 1))
if (( num == 1000 )); then
num=0
fi
echo "$num" > /var/run/myscript/last_value
...
printf -v t1 "%s>APX206,TCPIP*:T#%03d,060,000,000,000,000,00000000" "$user" "$num"
i'm trying to make script which will check if new deadlocks are created since last script run, if no new deadlock since last run it should print 0 if new deadlocks are created then it should print number
#DB profile
. /db2/tdb_inst/archinst/sqllib/db2profile;
#variable to get current deadlock count
a=`db2 get snapshot for all on archprd |grep Deadlock|head -1|awk '{print $4}'`
#variable to get last deadlock count
b=`cat /home/dbmon/script/darch`
#need your help to do below math in script
# if a$-b$ = 0 print 0 if a$-b$ > 0 print number
#after print export current deadlock count to darch file
echo $a > /home/dbmon/script/darch
TIA
tnt5273
need your help to do below math in script
if a$-b$ = 0 print 0 if a$-b$ > 0 print number
You can say:
(( a - b )) && echo $(( a - b )) || echo 0
This would print 0 if a = b else print a - b.
Actually, even the following should suffice:
echo $(( a - b ))
This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 10 years ago.
I am looping over the commands with for i in {1..n} loop and want output files to have n extension.
For example:
for i in {1..2}
do cat FILE > ${i}_output
done
However n is user's input:
echo 'Please enter n'
read number
for i in {1.."$number"}
do
commands > ${i}_output
done
Loop rolls over n times - this works fine, but my output looks like this {1..n}_output.
How can I name my files in such loop?
Edit
Also tried this
for i in {1.."$number"}
do
k=`echo ${n} | tr -d '}' | cut -d "." -f 3`
commands > ${k}_output
done
But it's not working.
Use a "C-style" for-loop:
echo 'Please enter n'
read number
for ((i = 1; i <= number; i++))
do
commands > ${i}_output
done
Note that the $ is not required ahead of number or i in the for-loop header but double-parentheses are required.
The range parameter in for loop works only with constant values. So replace {1..$num} with a value like: {1..10}.
OR
Change the for loop to:
for((i=1;i<=number;i++))
You can use a simple for loop ( similar to the ones found in langaues like C, C++ etc):
echo 'Please enter n'
read number
for (( i=1; i <= $number; i++ ))
do
commands > ${i}_output
done
Try using seq (1) instead. As in for i in $(seq 1 $number).
input from file $2 : 1 -> 2
while read -a line; do
if (( ${line[2]} > linesNumber )); then
echo "Graph does not match known sites4"
exit
fi
done < "$2"
For some reason inside the if condition, the value of ${line[2]) is not 2
but if I print the value outside if:
echo `${line[2]}`
2
What's linesNumber? Even if you put $linesNumber, where is it coming from?
If you are tracking the line number, you need to set it and increment it. Here's my sample program and data. It's inspired by your example, but doesn't do exactly what you want. However, it shows you how to setup a variable that tracks the line number, how to increment it, and how to use it in an if statement:
foo.txt:
this 1
that 2
foo 4
barf 4
flux 5
The Program:
lineNum=0
while read -a line
do
((lineNum++))
if (( ${line[1]} > $lineNum ))
then
echo "Line Number Too High!"
fi
echo "Verb = ${line[0]} Number = ${line[1]}"
done < foo.txt
Output:
Verb = this Number = 1
Verb = that Number = 2
Line Number Too High!
Verb = foo Number = 4
Verb = barf Number = 4
Verb = flux Number = 5