Shell script does not capture value but Command Prompt can capture and update the data in the variable

108 Views Asked by At

Issue Why the read_stat is not updated inside the script but works fine from Command Prompt ?

The actual implementation and output

Shell Script :Test1.sh

sg_raw -v -s 512 -i ttttt.bin /dev/sg4 12 00 00 00 &>read_stat

if grep -w "Good" read_stat; then

 printf "PASS"

else

printf "FAIL"

Output while running the script

$ sudo./Test1.sh

$ sg_raw -v -s 512 -i ttt.bin /dev/sg4 12 00 00 00

cdb to send: 12 00 00 00

SCSI Status: Good

FAIL

(base) dd@qqqq: ls -l

-rwxrwxrwx 1 dd dd 0 Jul 13 11:09 read_stat

EXECUTE THE COMMAND FROM TERMINAL

$ sg_raw -v -s 512 -i ttttt.bin /dev/sg4 12 00 00 00 &>read_stat

$ ls -l

-rwxrwxrwx 1 dd dd 41 Jul 13 11:15 read_stat

$ cat read_stat

SCSI Status: Good

No errors

Previous Implementation (Ignore)

Shell Script: Test1.sh

  1. Created a file as read_stat

-rwxrwxrwx 1 dd dd 0 Jul 13 10:14 read_stat

  1. Executed the below command from Test1.sh

    sg_raw -v -s 512 -i ttttt.bin /dev/sg4 12 00 00 00 &>read_stat

  2. ls -l from Command Prompt

    -rwxrwxrwx 1 dd dd 0 Jul 13 10:18 read_stat

But if the same command "sg_raw -v -s 512 -i ttttt.bin /dev/sg4 12 00 00 00 &>read_stat"

is executed from Command Prompt, then I can see the value inside read_stat

-rwxrwxrwx 1 dd dd 14 Jul 13 10:18 read_stat

Why the read_stat is not updated inside the script but works fine from Command Prompt ?

1

There are 1 best solutions below

0
Sweety On

Used 2>read_stat instead of &>read_stat. Able to capture the data in the variable !

Old Format : sg_raw -v -s 512 -i ttttt.bin /dev/sg4 12 00 00 00 &>read_stat

New Format : sg_raw -v -s 512 -i ttttt.bin /dev/sg4 12 00 00 00 2>read_stat