I am using below script ,
clear
tput cup 1
echo "1";
tput cup 2
echo "2";
tput cup 4
echo "3";
then Out put is coming like
1
2
3
If i use wrong script like ,
clear
tput cup 1
echo "1";
tput cup 2
echo ;"2";
tput cup 3
echo "3";
then out put is
1
3/var.sh: line 5: 2: command not found
This means in third line 3 printed and 2 line error also printed .
So is there any way to detect error line number to print 3 after error?
EDIT:
when I use -x , output is
[root#srinivas Installation]# sh -x var.sh
+ clear
+ tput cup 1
+ echo 1
+ echo
+ echo 3up 2
3 2
[root#srinivas Installation]# found
+ tput cup 3
If you run your shell script with the -x option, it'll show you each line as it executes.
This is because standard output and standard error are printed in different ways - Standard output is synchronous, meaning the lines will be printed in sequence and as they arrive, while standard error is asynchronous, which means it's printed whenever the terminal can fit it in. One way to fix this is to chunk standard error together with standard output:
/var.sh 2>&1
More details in Greg's wiki.
PS: All the semicolons in the file are unnecessary - If your commands are separated by newlines, semicolons will never be necessary.
That output looks like what should be expected. Here's the sequence:
tput cup 1
echo "1"
Move to line 1 and output a 1. This works properly.
tput cup 2
echo ;"2";
Move to line 2, then output a blank line (echo with no arguments) which moves to line 3. Then execute a program named 2, which the script can't find, resulting in the error message ./var.sh: line 5: 2: command not found printed on line 3.
tput cup 3
echo "3";
Move to line 3 and output a 3, which overwrites the . in the above error message. Resulting in the final apparent output that you note, even though it wasn't produced in a strictly left-to-right top-to-bottom order.
Related
I wrote a shell script to gather and show some information after a successful login.
However some info is taking some time to gather so I print to the terminal (ssh putty) ahead some headers and already available info before go back and print the delayed info into the right place.
To accomplish that I used the following script to get the current cursor position, (ignoring all the boring stuff that come before. It's a bunch of printf's, cat and cut's...
. ...
. ...
printf "^[[0m""\n"
# Get current settings.
if ! termios="$(stty -g 2>/dev/null)" ; then
echo "Not running in a terminal." >&2
exit 1
fi
# Restore terminal settings when the script exits.
trap "stty '$termios'" EXIT
# Disable ICANON ECHO. Should probably also disable CREAD.
stty -icanon -echo
# Request cursor coordinates
printf '\033[6n'
# Read response from standard input; note, it ends at R, not at newline
read -d "R" rowscols
# Clean up the rowscols (from \033[rows;cols -- the R at end was eaten)
rowscols="${rowscols//[^0-9;]/}"
rowscols=("${rowscols//;/ }")
#printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]} *<-- commented by me*
# Reset original terminal settings.
stty "$termios"
# To the stuff...
printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]}
line=${rowscols[0]}
line=$(($line - 10)) *<--- Indeed script's line 102. I want subtract 10*
col=56
printf '(r= %d, c= %d)\n' ${line} ${col} *<--- Printed two times, both times wrong values*
exit 1 *<--- Put here just to exit earlier*
## Get uptime/activetime formated to my taste.
m_activetime=$(/usr/bin/activetime -v)
printf "\33[%d;%dH^[[38;5;196m ${m_activetime}" ${line} ${col}
. ...
. ...
When I run the code i get:
. ...
. ...
. ...
||=-= _ |-=- |+++++++| _ ||= _ | :
`~‾‾ '--~~__|- = |+++++__|----~‾ ‾~`---', CPU stat⸱:
~---__|,--~' Weather⸱⸱:
(row 16, column 1)
./c.asc: line 102: 16 1 - 10: syntax error in expression (error token is "1 - 10")
(r= 16, c= 1)
(r= 56, c= 0)
lr#pi:~ $
1) Script is bash (shebang #!/usr/bash)
2) The line (row 16, column 1) seems OK!
3) The script is called c.asc
4) I wonder what the heck that error is, I've used similar expressions before, not with bash arrays but even so...
line 102: 16 1 - 10: syntax error
I can guess the 16, but where did it come the 1 - 10 ?
(error token is "1 - 10")
what token "1 - 10" ????!!!
5) The first (r= 16, c= 1) is already wrong, it should be (r= 6, c= 56). Why is this? What happened to the subtraction of 10? Where did it go the value of the variable col?
6) Even more strange. I didn't instruct to print a second time, even so, now the variable line has an identity crisis and display the col value, and in both cases the instruction col=56 seems to have been ignored. Why and how did the variable line get the value of the variable col? Why did the variable col shift from the wrong value 1, to the wrong value 0?
7) The showed script has been transformed to track the error. It started by not printing into the expected position, and display errors. Also a version of the the printf printf '(r= %d, c= %d)\n' $((${line} - 10)) ${col} display equally similar and bizarre error.
p.s.
After some additional experiments with only the part of the script to get the terminal cursor position, it seems also that it is not completely sane. It returns the position alright but trying things like read r c < <(curspos), (assuming that curspos is the name of the script that return the tuple lin col), the prompt hangs until Ctrl-C is pressed and after that prompt goes crazy.
Thank you
The issue is that you're quoting the value to the array.
rowscols=("${rowscols//;/ }")
This tells bash to ignore the spaces and consider it as one value. So when you get the first value with ${rowscols[0]} later, you actually get 16 1 instead of 16 and there's no second value.
It also worked with this printf because you didn't quote the values there.
printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]}
I don't know why it ran the last printf twice, but it seems to be solved with the quoting.
When I am executing the below script, I am getting the following error :-
The script executes infintely and below line is printed everytime.
"line 9: 1=1+2: command not found". Why?
#!/bin/bash
echo "Script 1 - Linux Scripting Book"
x=1
while [ $x -le 45 ]
do
echo x : $x
$x=$x+2
done
echo "End Of Script 1"
exit 0
Also if I change the $x=$x+2 to x+$x+2 then also I am getting the below error.
line 6: [: 1+2: integer expression expected
Same script when executed like this runs fine.
#!/bin/bash
echo "Script 1 - Linux Scripting Book"
x=1
while [ $x -le 45 ]
do
echo x : $x
let x=x+2
done
echo "End Of Script 1"
exit 0
You get line 9: 1=1+2: command not found because 1=1+2 is what $x=$x+2 is expanded into.
Use expr or let or ((...)) for integer calculations and bc for floating point:
let x=x+2
((x=x+2)) #same as above
((x+=2)) #same
((x++)) #if adding just one
((++x)) #if adding just one
x=$((x+2))
x=`expr $x + 2` #space before and after +
x=$(echo $x+2|bc) #using bc
x=$(echo $x+2.1|bc) #bc also works with floating points (numbers with decimals)
Since this part of the question isn't cleared yet, and not fine to post in a comment, I add this partial answer:
x=1; for i in 1 2 3 ; do x=$x+2; echo $x; done
1+2
1+2+2
1+2+2+2
As a side note: Don't use exit 0 at the end of your script without a good reason. When the script is done, it exits by itself without your help. The exit status will be the exit status of the last command performed, in your case a simple echo, which will almost always succeed. In the rare cases it fails, you will probably without intention hide that failure.
If you source the script, the exit will throw you out of your running shell.
But you can rewrite your while loop like this:
x=0
while (($((x)) < 9))
do
echo x : $x
x=$x+2
done
echo $((x))
x : 0
x : 0+2
x : 0+2+2
x : 0+2+2+2
x : 0+2+2+2+2
10
Because that's not the Bourne shell syntax for setting a variable; it looks more like Perl or PHP. The $ is used for parameter expansion and is not part of the variable name. Variable assignment simply uses =, and let evaluates arithmetic expressions (much like $((expression))). Another syntax that should work is x=$((x+2)). Note that these arithmetic evaluations are a bash feature; standard unix shells might require use of external tools such as expr.
Case scenario:
$ cat Status.txt
1,connected
2,connected
3,connected
4,connected
5,connected
6,connected
7,disconnected
8,disconnected
9,disconnected
10,disconnected
11,disconnected
12,disconnected
13,disconnected
14,connected
15,connected
16,connected
17,disconnected
18,connected
19,connected
20,connected
21,disconnected
22,disconnected
23,disconnected
24,disconnected
25,disconnected
26,disconnected
27,disconnected
28,disconnected
29,disconnected
30,connected
As can be seen, there are "hollows", understanding them as lines with the "disconnected" value inside the sequence file.
I want, in fact, to detect these "holes", but it would be useful if I could set a minimum n of missing numbers in the sequence.
I.e: for ' n=5' a detectable hole would be the 7... 13 part, as there are at least 5 "disconnected" in a row on the sequence. However, the missing 17 should not be considered as detectable in this case. Again, at line 21 whe get a valid disconnection.
Something like:
$ detector Status.txt -n 5 --pattern connected
7
21
... that could be interpreted like:
- Missing more than 5 "connected" starting at 7.
- Missing more than 5 "connected" starting at 21.
I need to script this on Linux shell, so I was thinking about programing some loop, parsing strings and so on, but I feel like if this could be done by using linux shell tools and maybe some simpler programming. Is there a way?
Even when small programs like csvtool are a valid solution, some more common Linux commands (like grep, cut, awk, sed, wc... etc) could be worth for me when working with embedded devices.
#!/usr/bin/env bash
last_connected=0
min_hole_size=${1:-5} # default to 5, or take an argument from the command line
while IFS=, read -r num state; do
if [[ $state = connected ]]; then
if (( (num-last_connected) > (min_hole_size+1) )); then
echo "Found a hole running from $((last_connected + 1)) to $((num - 1))"
fi
last_connected=$num
fi
done
# Special case: Need to also handle a hole that's still open at EOF.
if [[ $state != connected ]] && (( num - last_connected > min_hole_size )); then
echo "Found a hole running from $((last_connected + 1)) to $num"
fi
...emits, given your file on stdin (./detect-holes <in.txt):
Found a hole running from 7 to 13
Found a hole running from 21 to 29
See:
BashFAQ #1 - How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
The conditional expression -- the [[ ]] syntax used to make it safe to do string comparisons without quoting expansions.
Arithmetic comparison syntax -- valid in $(( )) in all POSIX-compliant shells; also available without the expansion side effects as (( )) as a bash extension.
This is the perfect use case for awk, since the machinery of line reading, column splitting, and matching is all built in. The only tricky bit is getting the command line argument to your script, but it's not too bad:
#!/usr/bin/env bash
awk -v window="$1" -F, '
BEGIN { if (window=="") {window = 1} }
$2=="disconnected"{if (consecutive==0){start=NR}; consecutive++}
$2!="disconnected"{if (consecutive>window){print start}; consecutive=0}
END {if (consecutive>window){print start}}'
The window value is supplied as the first command line argument; left out, it defaults to 1, which means "display the start of gaps with at least two consecutive disconnections". Probably could have a better name. You can give it 0 to include single disconnections. Sample output below. (Note that I added series of 2 disconnections at the end to test the failure that Charles metions).
njv#organon:~/tmp$ ./tst.sh 0 < status.txt # any number of disconnections
7
17
21
31
njv#organon:~/tmp$ ./tst.sh < status.txt # at least 2 disconnections
7
21
31
njv#organon:~/tmp$ ./tst.sh 8 < status.txt # at least 9 disconnections
21
Awk solution:
detector.awk script:
#!/bin/awk -f
BEGIN { FS="," }
$2 == "disconnected"{
if (f && NR-c==nr) c++;
else { f=1; c++; nr=NR }
}
$2 == "connected"{
if (f) {
if (c > n) {
printf "- Missing more than 5 \042connected\042 starting at %d.\n", nr
}
f=c=0
}
}
Usage:
awk -f detector.awk -v n=5 status.txt
The output:
- Missing more than 5 "connected" starting at 7.
- Missing more than 5 "connected" starting at 21.
This question already has answers here:
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 8 years ago.
I'm making a program in bash that creates a histoplot, using numbers I have created. The numbers are stored as such (where the 1st number is how many words are on a line of a file, and the 2nd number is how many times this amount of words on a line comes up, in a given file.)
1 1
2 4
3 1
4 2
this should produce:
1 #
2 ####
3 #
4 ##
BUT the output I'm getting is:
1 #
2 #
3 #
4 #
however the for loop is not recognising that my variable "hashNo" is a number.
#!/bin/bash
if [ -e $f ] ; then
while read line
do
lineAmnt=${line% *}
hashNo=${line##* }
#VVVV Problem is this line here VVVV
for i in {1..$hashNo}
#This line ^^^^^^^ the {1..$hashNo}
do
hashes+="#"
done
printf "%4s" $lineAmnt
printf " $hashes\n"
hashes=""
done < $1
fi
the code works if I replace hashNo with a number (eg 4 makes 4 hashes in my output) but it needs to be able to change with each line (no all lines on a file will have the same amount of chars in them.
thanks for any help :D
A sequence expression in bash must be formed from either integers or characters, no parameter substitutions take place before hand. That's because, as per the bash doco:
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.
In other words, brace expansion (which includes the sequence expression form) happens first.
In any case, this cries out to be done as a function so that it can be done easily from anywhere, and also made more efficient:
#!/bin/bash
hashes() {
sz=$1
while [[ $sz -ge 10 ]]; do
printf "##########"
((sz -= 10))
done
while [[ $sz -gt 0 ]]; do
printf "#"
((sz--))
done
}
echo 1 "$(hashes 1)"
echo 2 "$(hashes 4)"
echo 3 "$(hashes 1)"
echo 4 "$(hashes 2)"
which outputs, as desired:
1 #
2 ####
3 #
4 ##
The use of the first loop (doing ten hashes at a time) will almost certainly be more efficient than adding one character at a time and you can, if you wish, do a size-50 loop before that for even more efficiencies if your values can be larger.
I tried this for (( i=1; i<=$hashNo; i++ )) for the for loop, it seems to be working
Your loop should be
for ((i=0; i<hashNo; i++))
do
hashes+="#"
done
Also you can stick with your loop by the use of eval and command substitution $()
for i in $(eval echo {1..$hashNo})
do
hashes+="#"
done
Can someone explain why these echo commands doesn't output [#10] and so on?
# echo [#10]
1
# echo [#11]
1
# echo [#12]
1 2
# echo [#13]
1
# echo [#14]
1
You have a file named "1" and a file named "2" in your current directory.
The shell is performing pattern matching on the glob patterns before handing the results to echo. [#10] is a character class containing a #, a 1 and a 0.
See http://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching
If you want the literal [#10], etc, you have to enclose it in quotes, single or double doesn't matter.
(to answer the question in your last comment)
You could use the printf(1) command:
printf "Error: %s went wrong. Error code [#%d]\n" "something" $[10+2]
The $[10+2] is here to show how to do arithmetic in shell. You could replace "something" with e.g. $somevariable ...