0
str1="ace"
str2="com"
str3="ros"
$name = readinput(Enter name:)

if [[ "$name" == "$str1" ]]; 
then

run ace.txt
else
if [[ "$name" == "$str2" ]];
then

run commscope.txt
else
if [[ "$name" == "$str3" ]];
then

run rosgenberger.txt
else
echo "no match found"
fi
fi
fi

Script is not getting executed, please help. My desired action should be as follows

  • If user input is "ace" --- run ace.txt should be executed
  • If user input is "com" --- run commscope.txt should be executed
  • If user input is "ros" --- run rosgenberger.txt should be executed.

I am running this script in the path where ace.txt/commscope.txt/rosgenberger.txt scripts are present.

Kusalananda
  • 333,661
  • "Script is not getting executed" how are you trying to run it, and from what directory? – Chris Davies Jan 06 '21 at 08:38
  • 1
    What kind of shell are you using? The $name = readinput(Enter name:) instruction certainly looks wrong for most shells I know (space around =, command name instead of command substitution ...). I would recommend using shellcheck to debug your script; the tool is also available standalone on many Linux distributions. Also, please indicate exactly how the "script is not getting executed": is there an error message, or simply unexpected behavior? If so, which? Please edit your question to address these points, don't reply using comments. – AdminBee Jan 06 '21 at 08:47

2 Answers2

1

You likely want to do something like the following:

#!/bin/sh

printf 'Enter name: ' >&2 read -r name

case $name in ace) run ace.txt ;; com) run commscope.txt ;; ros) run rosgenberger.txt ;; *) printf 'No match for "%s"\n' "$name" esac

This reads a "name" from the user and executes a command based on the user's response.

In the bash shell, you could use something a bit slimmer:

#!/bin/bash

declare -A map=( [ace]=ace.txt [com]=commscope.txt [ros]=rosgenberger.txt )

read -p 'Enter name: ' -r name

if [[ -n ${map[$name]} ]]; then run "${map[$name]}" else printf 'No match for "%s"\n' "$name" fi

This sets up an associative array with the expected "names" as keys and the corresponding filenames as values. Depending on the user's input, the correct filename is used with the run command.

Most of the time, though, you don't want to interact with the user, but to allow the user to simply provide the input via a command line option. The following bypasses the interactive prompt for input by using the first argument passed via the command line as the default value for name:

#!/bin/bash

name=$1

declare -A map=( [ace]=ace.txt [com]=commscope.txt [ros]=rosgenberger.txt )

if [[ -z $name ]]; then read -p 'Enter name: ' -r name fi

if [[ -n ${map[$name]} ]]; then run "${map[$name]}" else printf 'No match for "%s"\n' "$name" fi

This would be used like this:

./myscript ace

... for example. The script would then bypass the interactive question and execute run ace.txt.

The code could be slimmed down further by letting variable expansion needed for the run command do our error reporting:

#!/bin/bash

name=$1

declare -A map=( [ace]=ace.txt [com]=commscope.txt [ros]=rosgenberger.txt )

if [[ -z $name ]]; then read -p 'Enter name: ' -r name fi

run "${map[$name]?Name $name not matched}"

This would output something like

line 15: map[$name]: Name Boo not matched

if the user entered the name Boo.

Kusalananda
  • 333,661
  • Consider exec run if there's no other commands after, and those error messages need >&2. But yeah, the case was exactly my first thought. – Toby Speight Jan 17 '21 at 16:34
0

Assuming you're using Bash shell, have a closer look at this line. There is no readinput() function defined so this fails:

$name = readinput(Enter name:)

Also, variable assignment cannot have whitespace on either side of the =. Assignments should not be prefixed with the $ character. maybe try this instead:

echo -n "Enter name:"
read name

Also, as @AdminBee alludes to, be sure to add the shebang at the beginning of your script

Toby Speight
  • 8,678
  • read -p 'Enter name: ' name, or, if you want to output the prompt with echo, at least output it to standard error with echo -n 'Enter name: ' >&2. See e.g. https://unix.stackexchange.com/questions/336983 – Kusalananda Jan 06 '21 at 09:08