Jump to content

Bash Script - Unmatching files

Go to solution Solved by WiiManic,

Since you haven't given a full path, I'd expect the file in L1 will be being outputted to the home folder of which ever cron user you've got running the job.

That will most likely be yours or the root user's home folder.

 

When you run it as your user manually (with ./gentest.sh), it will just default to making the file in whichever you are currently in.

 

Swapping targetFile to "/home/<USERNAME>/.ssh/testfilegen2.log" or to wherever you want the file to be stored sounds like it will fix it.

In general, always give a full file path, since it then means the file will always be where you expect it, no matter where you run the script.

Hi 

The below bash script runs every 10 mins on L1 and within the script is meant to generate a random password, then print that into a file, which is then copied to L2.

#!/bin/bash
targetFile="testfilegen2.log"
address="192.168.1.1"
netmask="255.255.255.0"
channel="1"
essid="GreRPi"
random=$(pwgen 13 -1)
        sudo echo "source-directory /etc/network/interfaces.d" > "$targetFile"
        sudo echo "auto lo" >> "$targetFile"
        sudo echo "auto wlan0" >> "$targetFile"
        sudo echo "allow-hotplug wlan0" >> "$targetFile"
        sudo echo "iface wlan0 inet static" >> "$targetFile"
        sudo echo "address $address" >> "$targetFile"
        sudo echo "netmask $netmask" >> "$targetFile"
        sudo echo "wireless-channel $channel" >> "$targetFile"
        sudo echo "wireless-essid $essid" >> "$targetFile"
        sudo echo "wireless-mode ad-hoc" >> "$targetFile"
        sudo echo "wireless-key s:$random" >> "$targetFile"
        sleep 1
        scp "$targetFile" pi@192.168.1.1:~/.ssh/
        sudo service networking restart

 

Unfornaltey L1 doesn't seem to recieve the new password but L2 does.

 

L1 Output

pi@raspberrypi:/etc/greenwich $ cat testfilegen2.log
source-directory /etc/network/interfaces.d
auto lo
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
wireless-channel 1
wireless-essid GreRPi
wireless-mode ad-hoc
wireless-key s:she5Bie1ojoon

 

L2 Output

pi@raspberrypi:~/.ssh $ cat testfilegen2.log
source-directory /etc/network/interfaces.d
auto lo
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
wireless-channel 1
wireless-essid GreRPi
wireless-mode ad-hoc
wireless-key s:ez2eeCeich0oc

 

Any Suggestions?

 

Thank you

Link to comment
https://linustechtips.com/topic/744103-bash-script-unmatching-files/
Share on other sites

Link to post
Share on other sites

For L1, you've not given a full path for the output, you've only given it a name.

Whereas for L2, you've at least said its in ~/.ssh.

 

Are you sure its outputting the log file on L1 to the location you think it is?

First thing I'd be doing is checking the time stamps of the files, to make sure both are updating like you expect.

If L2 is updating and L1 isn't then maybe the log file is being outputted somewhere else.

 

There isn't much else in the code that stands out, but since L2 is getting the correct file, then it seems like the correct file must be sat around some where.

Maybe try a find command as well?

 

CPU: 6700k GPU: Zotac RTX 2070 S RAM: 16GB 3200MHz  SSD: 2x1TB M.2  Case: DAN Case A4

Link to post
Share on other sites

2 hours ago, WiiManic said:

For L1, you've not given a full path for the output, you've only given it a name.

Whereas for L2, you've at least said its in ~/.ssh.

 

Are you sure its outputting the log file on L1 to the location you think it is?

First thing I'd be doing is checking the time stamps of the files, to make sure both are updating like you expect.

If L2 is updating and L1 isn't then maybe the log file is being outputted somewhere else.

 

There isn't much else in the code that stands out, but since L2 is getting the correct file, then it seems like the correct file must be sat around some where.

Maybe try a find command as well?

 

 

Thank you

I think it might have something to do with cron, as when I do ./gentest.sh (which is the name of the file in L1) it does all of the commands within the file correctly.

 

Thank you

Link to post
Share on other sites

Since you haven't given a full path, I'd expect the file in L1 will be being outputted to the home folder of which ever cron user you've got running the job.

That will most likely be yours or the root user's home folder.

 

When you run it as your user manually (with ./gentest.sh), it will just default to making the file in whichever you are currently in.

 

Swapping targetFile to "/home/<USERNAME>/.ssh/testfilegen2.log" or to wherever you want the file to be stored sounds like it will fix it.

In general, always give a full file path, since it then means the file will always be where you expect it, no matter where you run the script.

CPU: 6700k GPU: Zotac RTX 2070 S RAM: 16GB 3200MHz  SSD: 2x1TB M.2  Case: DAN Case A4

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×