bash script to read info from a file - bash

how can i make a bash script to tell the script this:
i will tell the bash like:
#!/bin/bash
include /usr/local/serverinfo.txt or.sh
rsync -avz $location root#$host:/$location2
all of this $location, $host , $location2, to be entered in /usr/local/serverinfo.txt
how can i tell the bash script to get this infos from the file,
if i put them in the same file will work just perfect, however i whant it to be outside of the file, any ideea?
let me know, thanks.

You can use source, or equivalently ., which takes another file as an argument and executes it. This assumes that the file you are sourceing contains valid bash syntax.
script.sh:
#!/bin/bash
var=1
source inc.sh # or . inc.sh
echo $var
inc.sh
var=2
output:
2

This depends on how the data in serverinfo.txt is formatted.
If it is a simple list like
/this/is/the/first/location
/this/is/the/host
/this/is/the/second/location
then you can do
location=$(sed -n '1p' /path/to/serverinfo.txt)
host=$(sed -n '2p' /path/to/serverinfo.txt)
location2=$(sed -n '3p' /path/to/serverinfo.txt)

Related

How can I log/read a string in a bash script without executing it?

I'm starting to learn how to write shell scripts. I have them all placed in a folder 'personal-scripts' in my home directory. They are starting to add up though. To solve this, I am attempting to create a script that loops over the directory and gives me a brief sentence about what each script does.
For now I can only output the script location and names via:
scriptsinfo
#!/bin/bash
for filename in ~/personal-scripts/*
do
echo $filename
done
Since I don't want to go back and update this file manually, I want to place the about sentence inside each other script either as a comment or string variable that can be evaluated.
How can I read the contents of each other script in the folder and output their string/info about what they do in this script?
You can do that using head command, which prints the first n lines of a file.
test.sh
# this is about line
# date is 14-9-2017
script data
..
..
~# head -n 2 test.sh
# this is about line
# date is 14-9-2017
If you add the description on each second line of your script, (after #!/bin/bash then let use sed -n "2p" $filenamein your script. You can also add a separator between each script:
#!/bin/bash
for filename in ~/personal-scripts/*
do
echo "---------------------"
echo $filename
echo "---------------------"
sed -n "2p" $filename
done
The alternative is to put the description anywhere, in a line starting by e.g # about: then you can grep it:
#!/bin/bash
for filename in ~/personal-scripts/*
do
echo "---------------------"
echo $filename
echo "---------------------"
grep "# about:" $filename | sed 's/# about://'
done
The | sed 's/# about://' is there to keep the description only.
If you have a comment inside all your scripts with a specific pattern such as
#info : This script runs daily
Then you can simply grep for the pattern and append to the name of the script in each line.
for filename in ~/personal-scripts/*
do
echo "$i : $(grep '#info' $i)"
done

Passing multiple arguments in a bash script

The simple script below does not work when, rather than passing a single file name, I want to pass multiple files through expansion characters like *
#!/bin/bash
fgrep -c '$$$$' $1
If I give the command script.sh file.in the script works. If I give the command script.sh *.in it doesn't.
Use "$#" to pass multiple file names to fgrep. $1 only passes the very first file name.
fgrep -c '$$$$' "$#"

How to save entire output of bash script to file

I am trying to get the entire output of a bash script to save to a file. I currently have one argument (ip address) at the beginning of the code looks like this:
#!/bin/bash
USAGE="Usage: $0 [<IP address>]"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
ip_addr=$1
What I'd like to do is add another argument called "output", that the entire output of the script will save to. I'm aware I could just run myscript.sh | tee textfile.txt, but I'd like to make the script a little easier to run for others.
Thanks in advance,
hcaw
After the usage message, add the following line:
exec > "$2"
This will redirect standard output for the rest of the script to the file named in the 2nd argument.
Then run using
myscript 192.0.2.42 output.txt

how to read data from a file in shell script

I have two shell script files test1.sh and test2.sh . I have another file called translogs.txt.
Now I need to copy the values of two variables in test1.sh to translog.txt and the same variables need to be copied to the corresponding values in test2.sh.
test1.sh
#!/bin/sh
ONE="000012"
TIME="2013-02-19 15:31:06"
echo -e "$ONE\n$TIME">translog.txt;
translog.txt
ONE="000012"
TIME="2013-02-19 15:31:06"
But here in test2.sh, I want the same value as in translog.txt to the corresponding variable
test2.sh
#!/bin/sh
ONE="000012"
TIME="2013-02-19 15:31:06"
1 Diry solution
$> cat translog.txt
ONE="000012"
TIME="2013-02-19 15:31:06"
With perl regular expression grep could match these value using lookbehind operator.
$> grep --only-matching --perl-regex "(?<=ONE\=).*" translog.txt
"000012"
And for TIME:
$> grep --only-matching --perl-regex "(?<=TIME\=).*" translog.txt
"2013-02-19 15:31:06"
So from withing the test2.sh script you can use it like this:
#!/bin/bash
ONE=`grep --only-matching --perl-regex "(?<=ONE\=).*" translog.txt`
TIME=`grep --only-matching --perl-regex "(?<=TIME\=).*" translog.txt`
2 Command line solution
Another solution pointed out in one of the links below would be to use:
the source (a.k.a. .) command to load all of the variables in the file into the current shell:
$ source translog.txt
Now you have access to the values of the variables defined inside the file:
$ echo $TIME
"2013-02-19 15:31:06"
3 Easiest solution
Another approach was mentioned by #user2086768. Put these lines to `test2.sh:
#!/bin/bash
eval $(cat translog.txt)
And as a result you would have assigned the two variables within the test2.sh script:
ONE="000012"
TIME="2013-02-19 15:31:06"
you can easily check that adding:
echo $ONE
echo $TIME
Check also these links:
how to get value of variable config in bash?
Here's a more general overview: Loading data into bash variables
As translog.txt is valid bash code, you could do:
source translog.txt
in test2.sh, ONE and TWO would be available in test2.sh.
A word of warning, that does open you up to shell script injection attacks if the values for ONE and TWO were to come from an untrusted source.
If your translog.txt is as you say, then this will work
#!/bin/sh
while read aa
do
eval "$aa"
done < translog.txt
eval should work for you.
Try to use this version of test2.sh:
test2.sh
#!/bin/bash
eval $(cat translog.txt)
echo $ONE
echo $TIME
This outputs:
000012
2013-02-19 15:31:06

Counting file lines in shell and in a script gives different results

For a bunch of files in a directory I want to get the number of lines for each one, store it
in a variable and do additional stuff. Via shell I can do it without problems if I do
read NLINES <<< $( cat file | wc -l )
but if I do it in a script
#!/bin/bash
for i in `ls *.dat `
do
read NLINES <<< $( cat $i | wc -l )
done
I get
Syntax error: redirection unexpected
Why the difference? How could I fix it?
I bet your default shell isn't bash but something else. Leave the #!/bin/bash and replace it with #!/bin/sh, to let your script use the default shell.
I made this error the other way, when I tried to use some debian scripts on Ubuntu, where #!/bin/sh behaved differently from my assumed #!/bin/bash.

Resources