Create file from script shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create file from script shell
# 1  
Old 05-31-2015
Create file from script shell

Hello all Smilie

Here is my code i try to complete:


Code:
  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.
# 2  
Old 05-31-2015
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-31-2015
Code:
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..
# 4  
Old 05-31-2015
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.
This User Gave Thanks to jgt For This Post:
# 5  
Old 06-01-2015
Quote:
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.
Even if not defined, address2 would overwrite the file - with nothing.

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

Quote:
Be aware that >> will append to an existing file.
Emphasize: append
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

3. UNIX for Dummies Questions & Answers

How to Create excel file(.csv) using shell script?

Hi, i have shell script which compiles n number of test cases and execute them one by one. i want to create report in excel through script in which two columns namely "test id" and "release".second column have two subcolumns namely compiles and excutes. so i want first column should display test... (15 Replies)
Discussion started by: RamMore123
15 Replies

4. Shell Programming and Scripting

How to create a shell script to read a foldername from a text file and go to the folder?

Hi, I am trying to write a shell script which can read folder names from a text file and then go to the folder and picks up a xml file and write on my sipp script so that I can run the sipp script. For example: I have a text file called thelist.txt where I have provided all the folders... (7 Replies)
Discussion started by: pm1504
7 Replies

5. Shell Programming and Scripting

Need help to create multiple file using shell script

HI, i created the below script to create the multiple files, iam not getting the required output, Please advice. #!/bin/sh v_date=$1 # argument will come as daymonthyear eg : 151112 v_day=`echo $v_date | cut -c 1-2` v_mon=`echo $v_date | cut -c 3-4` v_year=`echo $v_date | cut -c 5-6`... (4 Replies)
Discussion started by: jagguvarma
4 Replies

6. Programming

create a spool file based on values passed from korn shell to sql script

this is my issue. 4 parameters are passed from korn shell to sql script. parameter_1= varchar2 datatype or no value entered my user. parameter_2= number datatype or no value entered my user. parameter_3= number datatype or no value entered my user. parameter_4= number datatype or no... (5 Replies)
Discussion started by: megha2525
5 Replies

7. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

8. Shell Programming and Scripting

How to pass tablenames from a file to shell script to execute create statement in DB2

Hi, I am new to Shell Scripting, and I need to create nicknames for 600 tables in db2. I have the file names in a text file and i have to pass these table names to a shell script create nicknames in db2. Can some one please help me in this regard. (1 Reply)
Discussion started by: kamalanaatha
1 Replies

9. Shell Programming and Scripting

A shell script for create a a file in the respective path

Hello forum members, I have to create a out file in the current path./aaa/bbb/ccc/hhh. i am writing script below. ###script Begins##### #!/bin/ksh echo "Weclome" if then echo "Hello" rm -rf $aaa/bbb/ccc/hhh #clean the exsisting o/p file echo "no... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

10. Shell Programming and Scripting

how to create csv file using shell script

I have a file in multiple directory which has some records in the following format File: a/latest.txt , b/latest.txt, c/latest.txt -> Name=Jhon Age=27 Gender=M Street=LA Road Occupation=Service I want to generate a csv file from the above file as follows File: output.csv -> ... (9 Replies)
Discussion started by: rjk2504
9 Replies
Login or Register to Ask a Question