How to avoid the "temp files" in my script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to avoid the "temp files" in my script?
# 1  
Old 12-28-2016
How to avoid the "temp files" in my script?

Hi ! Smilie

I noticed that I create often of temporary files to keep character strings or other and I wish to avoid that if possible ? e.g :
Code:
#!/bin/bash
CONFIG_FILE="conf.cfg"
TEMP_HOSTNAME="temp_file1.txt"

for IP in `egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' $CONFIG_FILE`
do
        ssh "$IP" 'hostname' > $TEMP_HOSTNAME
        REMOTE_HOSTNAME=$(cat "$TEMP_HOSTNAME")
        echo ""$IP" "$REMOTE_HOSTNAME"" >> temp_file2.txt
done

If you have any suggestions ? Smilie
# 2  
Old 12-28-2016
Maybe this...

Code:
TEMP_HOSTNAME=$(ssh "$IP" 'hostname')

For temp_file2 you may store the information into an associative array(bash version >= 4.0) like this:

Code:
# declaration (important! automatic created, undeclared arrays or numeric-indexed arrays)
declare -A temphosts

# assignment
temphosts[$IP]="$TEMP_HOSTNAME"

# access it again:
echo ${temphosts[$IP]}

# iterate over associative array
for idx in ${!temphosts[@]} ;do echo ${temphosts[$idx]};done

This User Gave Thanks to stomp For This Post:
# 3  
Old 12-29-2016
Or, if you don't need the hostname further down the script and just want to collect the info in a file, try (untested):
Code:
for IP in $(egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' $CONFIG_FILE)
  do    echo ""$IP" $(ssh "$IP" 'hostname')  >> temp_file2.txt
  done

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

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

4. Solaris

How to avoid "cannot execute" issue with a runnable jar file?

I am trying to understand why the runnable jar file runs on one Unix server, but not the other with same environment settings. I copied exact same test jar from here --> http://www.javaworld.com/javaworld/javatips/javatip127/MakeJarRunnable.zip on to both Unix servers. Then changed... (5 Replies)
Discussion started by: kchinnam
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

7. Shell Programming and Scripting

Joining 3 AWK scripts to avoid use "temp" files

Hi everyone, Looking for a suggestion to improve the below script in which I´ve been working. The thing is I have 3 separated AWK scripts that I need to apply over the inputfile, and for scripts (2) and (3) I have to use a "temp" file as their inputfile (inputfile_temp and inputfile_temp1... (2 Replies)
Discussion started by: cgkmal
2 Replies

8. AIX

Probably an easy SMIT question- "Unable to open temp file"

Hi All, Can't find any documentation on the web for this anywhere, except about three web pages that are in Chinese. When I enter SMIT on this box, I get ERROR MESSAGE: Unable to open temp file I suspected smit.log, but it is universal readable, writeable by root, and I am root.... (6 Replies)
Discussion started by: jeffpas
6 Replies

9. Shell Programming and Scripting

split the string "Setview: arv-temp-view"

Hi experts, I would like to split the value of v and assign "arv-temp-view" to another variable d. I want to do it within shell script. My shell is "tcsh" v="Setview: arv-temp-view" split v and store d=arv-temp-view Please help Thanks Amit (2 Replies)
Discussion started by: amitrajvarma
2 Replies

10. Shell Programming and Scripting

Avoid "++ requires lvalue" Error in Loop Calculation

Hi All, Please help me to perform sum of values in a loop, I am getting following error: "total=0++432907765772: ++ requires lvalue" where actual statement is as : total=$total+$amt where amt can have +ve or -ve values Thanks Sandeepb (3 Replies)
Discussion started by: sandeepb
3 Replies
Login or Register to Ask a Question