1

If I have a program that multiplies the user-supplied value with 10, how do I write another bash script that has arguments passed to it and takes these arguments and collects the output of the other program and organizes it into a table?

For example:

    [user]$ table 1 5
    #
    #Value  Result
     1      10
     5      50
    [user]$
1
#!/bin/sh
tenTimes(){
  expr "$1" \* 10
}

table(){
  printf '%s\t%s\n' \#Value Result
  for a; do
    printf '%s\t%s\n' "$a" "$(tenTimes "$a")"
  done
}

table 1 5

Output:

#Value  Result
1   10
5   50
  • if my tenTimes script is in /home/user/sim/ but my table script is located in /home/user/scrips/ , how do I call the tenTimes script in the table script? – bbycakes3 Oct 26 '15 at 0:11
  • 1
    You run your tenTimes script in exactly the same way you would any other program or script. It has to be set executable (chmod +x), and it has to be either in your $PATH or you need to specify the full pathname to the script (e.g. /home/user/sim/tenTimes rather just tenTimes) – cas Oct 26 '15 at 0:15
  • /home/user/sim/tenTimes or have /home/user/sim/ be a component of your PATH variable. – PSkocik Oct 26 '15 at 0:15
  • Relative paths should work also, but you should cd into dirname "$0" (the path of your currently executing script) before you use them or graft them onto that path. – PSkocik Oct 26 '15 at 0:16
  • 1
    btw, if you intend to write a lot of scripts, it's useful to have a ~/bin directory and add that to your PATH. Then mv or cp your scripts to ~/bin – cas Oct 26 '15 at 0:17
0

You can it through a shell interpreter with an absolute or relative path;

/bin/sh /usr/local/bin/script
/usr/sbin/ksh ~/script

Maybe you want to use some functions much like an included library;

#!/bin/sh
func_a() { echo $(($1%42)); }

And in another file:

#!/bin/sh
. ~/path/to/func_a_file
func_a

Or maybe you want to call the script while assigning the results as a variable:

var1=$(~/path/to/script)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.