user1980835
user1980835

Reputation: 11

Create a script to save the file

I'm creating a Shell Script in which you want to save the command data to a file. The name of the file would be the name of the linux server obtained with the command hostname -f and the data would be the commands that will be executed in linux, cat example, ifconfig, etc. But I think I do not do it well, I'll give you an example. Thank you very much for your cooperation.

#!/bin/sh
server='hostname -f'
FILE="/root/'$server'.log"
df -hT
cd  /etc/sysconfig/
ls ip*
cat /etc/hosts

Upvotes: 0

Views: 1290

Answers (1)

PilouPili
PilouPili

Reputation: 2699

There is a subtle ditinction betwee using ` and using '

if you do

server='hostname -f'
echo server

the result will be

hostname -f

However if you do (and that's what you want)

server=`hostname -f`
echo server

the result will be

pilouraspi

(because my raspberry name is pilouraspi...)

After that I think you want to store data coming from other commands into the file so

#!/bin/sh
server=`hostname -f`
FILE=/root/$server.log
touch $FILE
df -hT >> $FILE
ls /etc/sysconfig/ip* >> $FILE
cat /etc/hosts >> $FILE

or in a single command

#!/bin/sh
(df -hT && ls /etc/sysconfig/ip* && cat /etc/hosts) > /root/`hostname -f`.log

Upvotes: 1

Related Questions