1

Very new to ubuntu here, and I have been assigned to create the prarg bash shell script below. Please bear with me I am trying to be very clear by providing all relevant examples.

ASSIGNMENT:
1. Modify the prargs program to precede each argument by its number. Thus, typing prargs a 'b c' d should give the following output:

1: a  
2: b c  
3: d  

The issue I'm having is the book example runs the following script by typing in "prargs a b c". When I type this I get the error: prargs:command not found. How do I get the script to run this way? The only way I can run the script is with ./prargs.sh or bash prargs.sh.

Script contents:

while [ "$#" -ne 0 ]  
do  
    echo "$1"  
    shift
done

Because I can't run it as directed, I am stuck. The directions and the book were very unclear, I can find snippets online but the main issue here is syntax for the entire code and I can't find that.

Help? and Thank you!

queenv
  • 23

2 Answers2

1

The main problem is the script has the wrong name of prargs.sh when the filename should be prargs. The second problem is there should be a "shebang" at the top telling the system it is a bash script:

#!/bin/bash

while [ "$#" -ne 0 ]  
do
    ((Count++))
    echo "$Count: $1"  
    shift
done

Note: Your script was missing the 1:, 2:, etc. parameter count. I've added that missing pieces with:

((Count++))
echo "$Count: $1"

So when you run the script you now see:

$ prargs a 'b c' d
1: a
2: b c
3: d

The next problem is calling the prargs without specifying a location in front. If you place your script in one of your paths it will be found without specifying a directory. To see your current paths use: echo $PATH. Many people will create a path in their home directory for personal scripts:

mkdir -p $HOME/bin

After this reopen the terminal to have it in your path. So create your script as $HOME/bin/prargs and then mark it executable:

chmod a+x $HOME/bin/prargs

After this no matter what your current directory is you can type prargs a 'b c' d.

  • thank you! You resolved EVERY issue, except I followed your script exactly and it still does not print with the numbers 1, 2, and 3. Help? – queenv Nov 30 '19 at 18:31
  • @queenv After initially posting the script I changed it an hour later to print the numbers. Do you have ((Count++)) line added to your script? Also after it works perfectly please click the grey check mark next to the answer so other people know it works. – WinEunuuchs2Unix Nov 30 '19 at 18:35
  • ah! yes it is flawless now! thank you SO MUCH!!! Also I did not know about the checkmark so thanks for that also :) – queenv Nov 30 '19 at 18:52
1

For this is necessary to have the script in a predefined path where the shell can find it.

By example, is possible create a new path in the home dir and use it to store scripts by the next steps:

  1. Create a folder with the name you want.
  2. Open .bashrc in the home directory(for example in the terminal type nano .bashrc).
  3. Move to the end of the file and copy paste the next: export PATH="/home/YOUR_USER_NAME/THE_FOLDER_CREATED_FOR_YOUR_SCRIPTS:$PATH"
  4. Reboot Now when you put something in the folder you created you can run it only with the name

NOTE: The script to be executed must have execution permission, to give it is as simple as the following command: chmod +x FILE_NAME

sandmor
  • 11