Bash - need to write data to a file based on output of a command in bash - linux

I have a bash script which checks IP Addresses in a file line by line and executes a command with that IP saved in a variable .
I wanted to add a few lines of bash which would write that IP to a new file when the output of the command is something specific.
I would be grateful if anyone shed some light on this matter.

Assuming you stored the output of the command to a variable named output, you can do something like:
# $output is the output you got
# $expected is the expected value
# $IP is IP you just checked
if [ "$output" == "$expected" ]; then
echo $IP >> file.txt
fi
replace file.txt with the name of the file name you prefer.

Related

How do I search for bash script files without having a specific extension within a folder?

I want to find bash script files under folders in Array.
But bash script files do not have a specified extension.
I wrote something like this:
for i in "${array[#]}"
do
# Here I will write the condition that the file is found in the folder $k
done
If your scripts have #!/bin/bash or #!/bin/sh in their first line (as they should), then you can use the file command to check if a file is a script or not.
For example, take this script:
#!/bin/bash
echo "I am a script!"
Output of file filename.sh will be filename.sh: Bourne-Again shell script, ASCII text executable, which is indicating it is a shell script. Note that the file command does not use the extension of the file to indicate its format, but uses the content of it.
If you don't have those lines at the beginning of your file, You can try to run every file (command: bash filename.ext) and the check if it was run successfully or not by checking the value of the variable ${?}. This is not a clean method but it sure can help if you have no other choices.
The file command determines a file type.
e.g
#!/bin/bash
arr=(~/*)
for i in "${arr[#]}"
do
type=`file -b $i | awk '{print $2}'`
if [[ $type = shell ]];then
echo $i is a shell script
fi
done

Capturing SSH Output in a variable in a bash script

I'm trying to SSH into another machine and caputre it's ip address and hostname into a variable.
However, the varaible seems to be empty when i echo it.
I have tried out answers from other posts, but it didnt solve my problem. I'm not able to figure out as what the problem is.
#!/bin/bash
FILE=/home/admin/Vishal/output.txt
input=host.txt
while IFS= read -r line
do
echo "$line"
if [ $line = $HOSTNAME ]
then
ip=`hostname -i`
domain=`hostname -A`
host=`hostname`
sudo echo $ip $domain $line localhost >> $FILE
else
output=$(ssh -i -t admin#$line << "ENDSSH"
ip2=`hostname -i`
domain2=`hostname -A`
host2=`hostname`
ENDSSH
)
echo $output
fi
done <"$input"
The input file contains a list of hostnames
The variable FILE contains the path of the file where the results are to be stored.
The varaible output is the one in which i want to store the results.
Note: The script works for first part of the if where ssh isnt required.
Ony this command is relevant for your quesion:
output=$(ssh -i -t admin#$line << "ENDSSH"
ip2=`hostname -i`
domain2=`hostname -A`
host2=`hostname`
ENDSSH
)
The command sets some variables, but doesn't produce any output, so it's expected that output does't contain your values.
If you really want three lines with hostname related values, then something simple like this should work
output=$(ssh admin#$line "hostname -i; hostname -A; hostname")

not able to use local variable outside the function after exporting

This is my script , even after using the export command not able to use variable outside of the block. Below is the code that i have tried. I also tried other option like declare -x var, but that is also not working.
Can someone please please comment on this , am i doing right ?
#!/bin/bash
{
var="123"
export var # exporting the variable so that i can access from anywhere
echo "var is "$var # able to get the value of this variable
} | tee log.txt
echo "var is "$var # not able to get the value of this variable
Because the pipe is causing the code between the braces to execute in a sub-shell you need to find a way to capture that data as opposed to storing it in a variable that is not accessible from the rest of the code. An example would be to store the output of a function in a variable, or to access it via command substitution. If you have script.sh as such:
#!/bin/bash
function get_pizza() {
echo "Pizza"
}
myvar=$(get_pizza)
printf "myvar is '%s'\n" $myvar
echo "Plain echo follows:"
echo $(get_pizza)
and then run bash script.sh you will get output as such:
[user#host]$ bash ./script.sh
myvar is 'Pizza'
Plain echo follows:
Pizza
Then if you still want to write to a file via tee, you can pipe your whole script to tee:
bash ./script.sh | tee foo.log
If you only want parts of the script to goto a file, you'll can also handle that with I/O redirection within the script: echo pizza > foo.log

Linux - Cat command doesn't maintain the file format

I have a Linux script that generates an HTML file with various outputs for various Linux commands.
Here is one of the outputs that creates an intrf.txt. I generated this file using this command
ip link show|sed '=;G'>intrf.txt
I did that as I want the lines of file to have line space between.
If I do the cat intrf.txt command on my shell I can see indeed the line spacing. If I run the script with the function below I see all lines of this file on my browser, but concatenated with no space between.
Maybe this is something simple, but I cannot figure it out.
function net_ifconfig
{
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "<h2 style="background-color:#00FF00"><font size="5"> CHECK 2. LIST OF AVAILABLE INTERFACE</h2>"
ip link show|sed '=;G'>intrf.txt
cat intrf.txt
else
echo "CHECK 1. INTERNET IF OFFLINE"
echo "<h2 style="background-color:#FF0000"><font size="5"> INTERNET IS NOT CONNECTED</h2>"
fi
}
Seems if I put the command between pre tags it works.
echo "<pre>"
ip link show|sed '=;G'>intrf.txt
cat intrf.txt
echo "</pre>"

Storing directory as a variable for later use in linux script

In my script, I am holding the location (path) of a file as a variable.
For example, fileA
An example of its contents are
fileA=/usr/anotherfolder/somefold/"filenamehere"
However, when i call a command on the file in the script such as:
cat $fileA
or
cat "$fileA"
I get an error saying the file or directory doesn't exist. If I echo $fileA to see what the output is, and then run a cat manually from the terminal, it works fine, don't know what is going wrong. Any help?
Some debug info:
fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
echo '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"
'[' '!' -r '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' ']'
For these particular lines
Check for readable file
echo $fileA
if [ ! -r "$fileA" ]
then
o=`expr $o + 1`
echo "$fileA not readable."
continue
fi
If file name is new(not "new"), then change
fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
to
fileA=/home/jacob/Desktop/CS35L/WORK/2/hw/test3/new

Resources