0

Like in the Title I tried to write a bash loop with if and else settings but while the if part works for now but the else part does not do anything it should

 #!/bin/bash
 
 x=30
 echo "Input No."
 read y
 z=29-y
 
 
 if [ $y > 29 ]
 then
 while (( $x <= $y ))
 do
 echo "TEST-No.$x" >> /home/TEST/test
 x=$(($x+1))
 done
 else
 while (($z>0))
 do
 sed -i '$d' /home/TEST/test
 z=$(($z-1))
 done
 fi

cat /home/TEST/test

if y>29 then it should write the line TEST-No.$x into the file test till x<y

root@TEST:/home/TEST# ./loop
Input No.
30
TEST-No.1
TEST-No.2
TEST-No.3
TEST-No.4
...
TEST-No.29
TEST-No.30

if y<=29 then if should delete the last line of the file till z = 0

root@TEST:/home/TEST# ./loop
Input No.
10
TEST-No.1
TEST-No.2
TEST-No.3
TEST-No.4
...
TEST-No.29
Hajiko
  • 1
  • 1
  • 2
    1st issue ... `z=29-y` doesn't do what you think it does; to see what's actually stored in `z` add `typeset -p z`; 2nd issue ... `[ $y > 29 ]` isn't a test but a redirection into a file named `29`; recommend you cut-n-paste the entire script (including shebang) into [shellcheck.net](https://www.shellcheck.net/) and make the suggested changes; NOTE: shellcheck won't find/report all issues (eg, `z=29-y`); you could also run the code in debug mode (`set -xv`; `...code...`; `set +xv`); I'd also recommend adding some indentations to make the code easier to read – markp-fuso Dec 19 '22 at 15:52

0 Answers0