put script into file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers put script into file
# 8  
Old 11-18-2011
Code:
 vi 007
#!/bin/bash
FILE=$1

if [ -f $FILE ];then

  echo " hello "  # say hello to the user
  echo " $HOME "
  echo " $HOSTNAME "
  echo " $(uname) "
  echo " today is $(date) "
  echo " $(whoami) is currently logged in "

else
  touch file.007

fi

~
~
~
~
~
"007" [New file] 16 lines, 247 characters 
an12:/home/vbe $ chmod 750 007
an12:/home/vbe $ ./007 >file.007
an12:/home/vbe $ ./007 file.007 >file.007
an12:/home/vbe $ rm file.007
an12:/home/vbe $ ./007 file.007 >file.007
an12:/home/vbe $ more file.007
 hello 
 /home/vbe 
 an12 
 AIX 
 today is Fri Nov 18 17:08:06 NFT 2011 
 vbe is currently logged in 
file.007: END

For you now, I tested both cases with same results, Why?
Code:
an12:/home/vbe $ ./007 >file.007
an12:/home/vbe $ ./007 file.007 >file.007

This User Gave Thanks to vbe For This Post:
# 9  
Old 11-18-2011
thanks.

would you recommend a script book for me so I can post less dummy questions Smilie
# 10  
Old 11-18-2011
In your code, if FILE does not exist, the script creates it (an empty FILE) and exit. You'll have to run your script twice to have what you expect. I don't know if it's a wanted behaviour.
If you redirect (>>) every time your script runs, the content of all your commands will be appended at the end of the FILE. If you want to start with a fresh FILE every time your script runs, you have to redirect (>) the first echo so FILE won't grow up in size.
Quote:
Originally Posted by me.
and my question is can these code be changed so I don’t have to type " >> " in every line?
Sure, group your commands between { (don't forget the space or new line just after) and } (don't forget space, semi-colon (;) or new line just before) and redirect (>) to FILE.
Code:
#!/bin/sh
{
echo " hello "
echo " $HOME "
echo " $HOSTNAME "
echo " $(uname) "
echo " today is $(date) "
echo " $(whoami) is currently logged in "
} > "$1"

exit 0


Last edited by tukuyomi; 11-18-2011 at 03:51 PM.. Reason: Disabled smileys
# 11  
Old 11-18-2011
great, thanks
that is what I was thinking about the " { . .. . . . . } exit 0 "
# 12  
Old 11-19-2011
Quote:
Originally Posted by me.
would you recommend a script book for me so I can post less dummy questions Smilie
Bash Guide for Beginners
BASH Programming - Introduction HOW-TO
Advanced Bash-Scripting Guide
# 13  
Old 11-24-2011
thanks kalak, will have a look on them at least on the if-then, loops, parts for a start....
 
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 to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

2. Shell Programming and Scripting

i writing a unix script but i want to out put a file and append on it.

i have an existing script that is used to send an e-mail containing the alrams that appear on the server. But i need to create a daily log file containing all the alarms that was send that day. i tired to add at the and of the script a command, echo command but for some reason the file was... (1 Reply)
Discussion started by: ashraf_victory
1 Replies

3. UNIX for Advanced & Expert Users

put data in excel file using shell script

Hi. I wish to add data in a specific excel file on daily basis.However the currect dat's data should always come on top i.e for example should always occupy cell A7,B7,C7 .. and the data of day before which was earlier on 7th row of each coloumn should move to 8th row..data on 8th row should... (1 Reply)
Discussion started by: kanus
1 Replies

4. Shell Programming and Scripting

Script to put block comment after finding regex in xml file

hi, i need my bash script to find regex in xml file.. and comment 2 lines before and after the line that contains regex.. can't use # needs to be <!-- at the beginning and --> and the end of the comment. so eg.. first block <filter> <filter-name>MyRegEx</filter-name> ... (11 Replies)
Discussion started by: Poki
11 Replies

5. Shell Programming and Scripting

how to put data using shell script to a excel file

Hi, Can any one tell me how to put data using shell script to a excel file from text file to other columns of excel file,leaving first column unaffected i.e it should not overwrite data in first column. Say my text file data is: 15-dec-2008 15-dec-2009 16-dec-2008 16-dec-2009 say my first... (1 Reply)
Discussion started by: siri_886
1 Replies

6. Shell Programming and Scripting

Could I put this into a case script instead?

Hi guys, I have a script which clears down a range of directories, however it can take a while to run. Is there a way I can simplify the below, perhaps adding it into a case statement? client_directories=/usr/local/production/cleanup/bin/client_cleanups/client_directories.txt # clear down... (2 Replies)
Discussion started by: JayC89
2 Replies

7. Shell Programming and Scripting

ftp put in shell script -- whole file doesn't upload

Hi I'm having some trouble with a bash shell script that I'm writing. In the script, I'm trying to upload a file to a backup repository using ftp, but the whole file doesn't get uploaded. This is the file's properties at the start (I've highlighted the file size in red): -rw-r--r-- 1 root... (2 Replies)
Discussion started by: Viola
2 Replies

8. UNIX for Dummies Questions & Answers

Where should I put the script?

Hi Guys, I'm a new to the UNIX. Let say I have a 1 simple script to stop and start vnc service. I want to use that script for support because my network connection to my client side is not stable and every time I have to use a long command to restart vnc. Where should I put that script?... (3 Replies)
Discussion started by: akuslive
3 Replies

9. Shell Programming and Scripting

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (2 Replies)
Discussion started by: shilendrajadon
2 Replies

10. UNIX for Advanced & Expert Users

Script to search a bad record in a file then put the record in the bad file

I need to write a script that can find a bad record (for example: there is date field colom but value provided in the file for this field is N/A) then script shoud searches this pattern and then insert the whole record into the bad file. Example: File1 Name designation dateOfJoining... (1 Reply)
Discussion started by: shilendrajadon
1 Replies
Login or Register to Ask a Question