Shell Command Question - Superceded

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
gkelley
Level 1
Level 1
Posts: 26
Joined: Mon Aug 21, 2017 7:34 pm

Shell Command Question - Superceded

Post by gkelley »

See new post - File Empty When Script Running

Probably not Mint related but this forum is so great for getting answers I thought I'd try here. Mods delete if inappropriate.

I'm trying to run two commands in shell script.

First command runs a process that should detach as a daemon and sends output to a file:

bin/lt.js --host http:/somedomain.com:9999 --port 4444 > recvaddr.tmp &

The command after this tests for file and if exists displays the contents in an xterm window:

xterm -geometry 100x30 -hold -fa 'Monospace' -fs 14 -e $HOME/testaddr

testaddr script:

#!/bin/sh

file="$HOME/tunnel-client/recvaddr.tmp"
echo $file
if [ -e $file ];
then
cat $file
else
echo "Tunnel Closed"
fi

exit

The recvaddr.tmp file is created and has one line (output from daemon) but the xterm window only shows the file from echo and no file contents

I changed the name of the file capturing the output and testaddr correctly showed 'Tunnel Closed' so it isn't processing 'cat $file' correctly

I can open a new terminal and cat the file just fine.

Tried cat $HOME//tunnel-client/recvaddr.tmp and even replaced $HOME with actual path and it's always blank. Totally stumped why it's behaving like this.

tail doesn't work there either. I can replace cat $file with echo "here' and it shows so if-then working.
Last edited by LockBot on Thu Feb 09, 2023 11:00 pm, edited 5 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
deck_luck
Level 7
Level 7
Posts: 1577
Joined: Mon May 27, 2019 6:57 pm
Location: R-4808 North

Re: Shell Command Question

Post by deck_luck »

Maybe the originally process has not had time to put the content in the file you are checking. You might try as a test to add sleep 6 ; ls -l ${file} in your if statement before the cat ${file} statement.
🐧Linux Mint 20.3 XFCE (UEFI - Secure Boot Enabled) dual boot with Windows 11

Give a friend a fish, and you feed them for a day. Teach a friend how to fish, and you feed them for a lifetime. ✝️
User avatar
karlchen
Level 23
Level 23
Posts: 18235
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: Shell Command Question

Post by karlchen »

Hello, gkelley,

the first command, bin/lt.js --host http:/somedomain.com:9999 --port 4444 > recvaddr.tmp &, creates the file recvaddr.tmp in the current directory, whichever this current directory is.

The script looks for the file recvaddr.tmp in a specific directory, however: $HOME/tunnel-client

I suspect that there are 2 files recvaddr.tmp:
+ one in $HOME/tunnel-client, empty file
+ one in $HOME, from where you seem to launch the bin/lt.js commandline

Resolution:
Make sure that the first command really writes the file to the same directory.
bin/lt.js --host http:/somedomain.com:9999 --port 4444 > $HOME/tunnel-client/recvaddr.tmp &

Regards,
Karl
Image
The people of Alderaan have been bravely fighting back the clone warriors sent out by the unscrupulous Sith Lord Palpatine for 792 days now.
Lifeline
Locked

Return to “Scripts & Bash”