I am a Unix/Linux shell script newbie and I have been asked to look at a script which contains the lines below. The following details in this question are vague but the person who wrote this code left no documentation and has since demised. Can anyone advise what they actually do?
There are two specific pieces of code. The first is simply line source polys.sh
where polys.sh
is a text file with contents:
failure="020o 040a"
success="002[a-d] 003[a-r] 004[a-s] 005[a-u]
Representing various parameters, I think, to do with the calculations the shell script performs. The nature of the calculations is, I am told, not important because the aim is to just get the script running.
The second piece of code is below and the relevant lines are delimited by Start
and Stop
comments. What I can tell you is that: $arg1
is blank, $opt1
is also blank, $poly
is the path and name of a text file and ./search
I believe to be a folder.
if [ $search == "yes" ]
then
# Search stage for squares containing zeros
#
# Start.
output="$outputs/search/"`basename $poly`
./search $opt1 $arg1 < $poly 2>&1 | tee $output
if tail -n1 $output | grep -v "success"
# End.
then
echo "SEARCH FAILURE" >> $output
continue
fi
# Save approximations
#
echo -n "SEARCH SUCCESS " >> $output
cat /tmp/iters >> $output
cp /tmp/zeros $inputs/search/`basename $poly`
else
echo "No search"
fi
EDIT Initial disclaimer as advised by Mr. Charles Duffy:
The below explanations assume you won't hit expansion-related bugs; please correct your code as advised by shellcheck.net to be assured that these explanations are correct
source polys.sh
includes the code from the script polys.sh
, which is a file in the same folder as the file sourcing it (hence just the filename, without its path). Within that file:
failure="020o 040a" success="002[ad] 003[ar] 004[as] 005[au]"
are two variable declarations; the variable $failure
is set to "020o 040a"
and $success
to "002[ad] 003[ar] 004[as] 005[au]"
. As the file was source
d, these two variables are available in your script (do echo "$failure"
and echo "$success"
to see for yourself).
output="$outputs/search/`basename $poly`"
has two parts to explain:
"$outputs/search/"
sets the variable $output
to "$outputs/search/"
, ie, to the value of the variable $outputs
, appended by the string "/search/"
.,
`basename $poly`
anything in backticks is a command substitution, which interprets and runs the command returning its output, and the command basename $poly
gets the base file or folder name from $poly
, if it is a file path (eg, basename $poly
for poly="/dev/file.txt"
yields file.txt
); the output is appended as a string. to "$outputs/search/"
.
./search $opt1 $arg1 < $poly 2>&1 | tee $output
./search $opt1 $arg1 < $poly 2>&1 | tee $output
is two commands, separated by a pipe |
:
./search $opt1 $arg1 < $poly 2>&1
runs the executable file ./search
( ./
is shorthand for the current script's directory) with two arguments, $opt1
and $opt2
variables. $poly
is the variable name which should represent a file path, of which the file path has its content redirected to the command (using <
). The output of all errors ( stderr
, as 2
) is redirected ( >
) to the standard output ( stdout
, or &2
, the ampersand represents this is a file descriptor, not a file path, otherwise it would redirect output to a file named 2
).
tee $output
tee
pipes outputs stdin
to stdout
and to arguments as file paths. So tee "/home/nick/output"
would save the stdin
to a file at "/home/nick/output"
, as well as the stdout
.
if tail -n1 $output | grep -v "success"
tail -n1 $output
gets the last line of the file at the "$output" variable's value.
grep -v "success"
searches for any non-match ( -v
inverts the match) in the last line from tail -n1
of "success"
in a line (eg, if the last line is "fail"
, it would pass the if
statement as it does not contain "success"
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.