0

So I have a shell script math.sh which takes a number as an argument and echos one added to it and one subtracted from it:

#!/bin/bash

echo "Add: "$(($1 + 1)) echo "Subtract : "$(($1 - 1))

and my other shell script execute.sh is basically taking math.sh and a textfile as an argument and writing the output of math.sh to the text file.

#! /bin/sh
echo $1 > $2

However, the two echos are outputting to the text file on the same line as: Add: $(($1 ++)) Subtract : $(($1 --)) when I need it on separate lines like:

Add:$(($1 ++))

Subtract:$(($1 --))

How would I do this without editing math.sh? Because my execute.sh needs to be able to output any shell script to the text file, not just math.sh, on separate lines.

  • Please add the code of the other shell script that is doing the printing, that's what is probably causing the bug (e.g. you could be echoing an unquoted variable). – user000001 Apr 20 '21 at 07:41
  • @user000001 hi, I've added the code now – no_bashell Apr 20 '21 at 07:45
  • Your math.sh script has errors in it (you can't change $1 with ++ and --). Do you want your other script to take math.sh as its first argument, run it, and write the result to the second argument? If so, what about the argument that math.sh takes? – Kusalananda Apr 20 '21 at 07:51
  • @Kusalananda sorry, it was actually +1 and -1, theres multiple argument files Im trying to use execute.sh to run and didn't look at math.sh when writing my question, I just wrote it on the spot thinking I remembered its contents. – no_bashell Apr 20 '21 at 07:55
  • and also http://mywiki.wooledge.org/WordSplitting. echo $foo in effect changes all runs of whitespace in $foo to single spaces. Plus expands globs. – ilkkachu Apr 20 '21 at 09:00

1 Answers1

-1

The problem is the missing quotes, it should be:

#!/bin/sh
echo "$1" > "$2"

This is needed because otherwise bash will process any special characters inside the variable, such as spaces, newlines, asterisks (*), etc.

In this case the newlines were braking the argument into multiple arguments, causing echo to join them with a space.


Note that this is printing the first argument itself to the file.

To print the contents of $1 you would do:

#!/bin/sh
cat "$1" > "$2"

to print the output of $1 you would do:

#!/bin/sh
./math.sh "$1" > "$2"

If you also want the changes of the variables to be reflect in the current script, you can source it:

#!/bin/sh
. "$1" > "$2"
user000001
  • 3,635
  • Thank you so much!! I have been going crazy over this, I appreciate the help:) – no_bashell Apr 20 '21 at 07:48
  • This does not write the output of math.sh (if given as first argument) to anywhere. It may write the string math.sh to a file, but it would have done without the quoting fix as well. – Kusalananda Apr 20 '21 at 07:49
  • @Kusalananda: I was going by OP's source code, and sample output (for previous revision). I added a couple of more examples, not sure what OP is really after. – user000001 Apr 20 '21 at 07:53
  • Note for your last script: The math.sh script takes an argument. – Kusalananda Apr 20 '21 at 07:54
  • 1
    Sorry for the confusion, yes execute.sh takes another argument which it passes to math.sh. math.sh is just one of multiple files i need to pass to execute.sh so I didn't carefully look at math.sh's code, I just knew my consistent recurring problem was the lines kept outputting to the text file on the same line for math.sh and every other .sh file I was passing to execute.sh so @user000001 did fix that problem – no_bashell Apr 20 '21 at 08:01