Simple interactive bash script, help needed!


Recommended Posts

Hi guys!

 

I have created a very long bash script for a pipeline that can make my work semi-automatic, but I'm new to bash scripting, thus I'm not familiar with many of the common commands.

 

So this is what I'd like to do.

 

The script should ask the user at the beginning if he/she has CUDA compatible GPU or not. If the answer is yes then after some preprocessing steps the script will initiate the gpu compatible command (lets call it "gpu_compatible", if it isn't then it will do it without gpu (call this "no_gpu").

 

I know, that I can make an alias for the gpu compatible/noncompatible stuff and write an if condition eg.:

 

alias gpu="run the gpu_compatible command"

alias no_gpu="run the no_gpu compatible command"

 

read - p "Do you have a CUDA compatible GPU (y/n): "

if [  $REPLY == "y" ]; then

gpu;

elif [  $REPLY == "n" ]; then

no_gpu;

fi

 

What my problem is, that there are many steps that should be run during preprocessing to create the necessary files for running the gpu/no_gpu command, but this question has be asked at the beginning!

 

How can I make the script to work like this:

 

1. question (yes/no)

2. step 1 - creates files for step 4

3. step 2 - creates files for step 4

4. step 3 - creates files for step 4

5. step 4 - initiates the command according to the answer in "1."

 

Thank you for your help!

 

Could you have an advice where can I learn these things?

I'm only at the beginning of this whole programming/scripting process and even while scratching only the surface I can see the possibilities how much this can help my work.

 

 

 

 

Link to comment
Share on other sites

I am not sure if i am missing what your trying to do here but

read - p "Do you have a CUDA compatible GPU (y/n): "
if [  $REPLY == "y" ]; then
command1
command2
command3
gpu
elif [  $REPLY == "n" ]; then
command1
command2
command3
no_gpu;
fi
Link to comment
Share on other sites

So the only way is to duplicate the whole process before the gpu command? There are 128 lines of commands before entering the gpu analysis phase.

Link to comment
Share on other sites

Just fixing haggis answer :)

read - p "Do you have a CUDA compatible GPU (y/n): " REPLY

if [  $REPLY == "y" ]; then
command1
command2
command3
gpu

elif [  $REPLY == "n" ]; then
command1
command2
command3
no_gpu
fi
Link to comment
Share on other sites

So the only way is to duplicate the whole process before the gpu command? There are 128 lines of commands before entering the gpu analysis phase.

 

 

you could have another alias with all the commands and just call the alias

Link to comment
Share on other sites

How about taking advantage of Bash functions to achieve your goal?

 

#!/bin/bash

perform_prerequisite_tasks()
{
    # Run any commands that need to be run before the gpu-dependent command.
    command1 
    command2
    command3
    ...
    command128
}

# ------------------------------------------------------------------------------

gpu()
{
    # CUDA-oriented command.
    echo run the gpu_compatible command
}

# ------------------------------------------------------------------------------

no_gpu()
{
    # CPU-oriented command.
    echo run the no_gpu compatible command
}

# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# ENTRY POINT

read -p "Do you have a CUDA compatible GPU (y/n): " has_gpu_response

# Sanity check the user response.
if ! echo "$has_gpu_response" | grep '^[YyNn]$' >/dev/null; then
    echo "Invalid response. Exiting..." >&2
    exit 1
fi

# Do prerequisite tasks regardless.
perform_prerequisite_tasks

# If user indicates CUDA compatible GPU is installed, run GPU-oriented command,
# otherwise run the CPU-oriented command.
if [ "$has_gpu_response" == 'Y' ] || [ "$has_gpu_response" == 'y' ]; then
    gpu
else
    no_gpu	
fi
The code above uses three functions:
  • perform_prerequisite_tasks: Run the commands that are required before the maybe-GPU-oriented command runs.
  • gpu: The CUDA command alias, re-written as a function.
  • no_gpu: The non-CUDA command alias, re-written as a function.
Link to comment
Share on other sites

This what I did:

 

Created a "function" for each and every independent command block, called those functions with an if condition, and I made some kind of nested functions also with more ifs :woot: .

 

Now the script asks 2 questions and executes the whole thing according to my needs. No duplicates, function calls only, some processes running parallel. Great stuff, made my life much easier!

 

Thank you guys!

Link to comment
Share on other sites

This topic is now closed to further replies.