Create file from script shell

Hello all :slight_smile:

Here is my code i try to complete:

  address1="$(ssh  root@$machine -x "lxc-info -n $machine-worker1  -H -i")" 

if[!test -e addrfile ] //ifthe file addrfile does not exist  

then create the file addrfile 

echo "$address1">"$addrfile" 

fi 

"$address1">"$addrfile" 

  

How, can i create file from script shell ?

Thanks a lot.

Best Regards.

A non-existent file will be created upon redirection, i.e. both the operators > and >> will open a "fresh" file for output. Be aware that >> will append to an existing file.

touch filename will create an empty file independently.

address1="10.0.1.4"
if [ ! -f addrfile.txt ]; then
    echo "$address1" > "addrfile.txt"
fi
echo "$address2">"addrfile.txt"
echo "read address from file: "
echo $address2

it creates the file and display only the message read address from file without address2 ! Have you an idea please. (my goal is to store and get a variable in/from a file..

In line 3: you do not need quotes around the file name.
In line 5: address2 is not defined. and again the file name does not need to be in quotes. If address2 had been defined, it would erase the work done by line 3.
In line 7: You should use "cat" to display the contents of a file; "echo" to display the contents of a variable.

Even if not defined, address2 would overwrite the file - with nothing.

---------- Post updated at 10:17 ---------- Previous update was at 10:16 ----------

Emphasize: append