LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   BASH Shell Scripting (http://www.linuxquestions.org/questions/linux-general-1/bash-shell-scripting-526830/)

tekmann33 02-08-2007 03:16 PM

BASH Shell Scripting
 
I am relatively new to bash shell scripts, but what I wanted to do was write an interactive shell script that would prompt a user to enter a number that corresponds to the day of the week [Mon = 1, Sun = 7] and then label a backup tape ISD00[1-7] based upon what the user chose. If the user chose 2, the tape would be labeled ISD002.

I have the code logic and the proper mount and relabel commands worked out already. What I wanted to know is if bash has a syntax for attaching numeric data to a string. To be more specific, I want the user to enter a number (1-7) and I want to take that number and attach it to a string ('ISD00 ') and then assign that to a variable (variable = 'ISD00' + number). I will them use the variable within my mount and relabel commands to refer to the tape.

I could write a case statement for this, but I was wondering if there was a more efficient way to accomplish this task.

Any input would be appreciated.

colucix 02-08-2007 03:24 PM

Variables in bash have not a specific type, or better they are treated as numeric if they contain only digits and if the context requires a numeric operation or a numeric comparison. Otherwise they are all treated as character strings. So you can "append" the value entered by the user simply as:
Code:

#!/bin/bash
prefix=ISD00
read number
label=$prefix$number

Writing them in sequence does a string concatenation!

Junior Hacker 02-08-2007 04:40 PM

If this is in an office environment, have you included the "Human factor" in your scripts. When an employee is irritated, or got up on the wrong side of the bed, or it is a Monday "people hate Mondays", or they just want to tick you off. What's to say they will not punch in the right numerical value but punch in an incorrect value, and when confronted, they simply reply "Oh! I must have punched the wrong one, sorry, I'll try not to do it again".
Or possibly this script has something to do with taming such attitudes.

tekmann33 02-08-2007 04:53 PM

I have built some checks and balances in the logic, like a user may enter only numbers 1 - 7 and then repeat the prompt until they enter the correct number. However, I do not have anything to ensure a number is entered. Does bash have something like a 'isnum' function that I can check user input with?

unSpawn 02-08-2007 05:03 PM

You could "declare -i var" then read in var and if var is zero it's wrong anyway ;-p


All times are GMT -5. The time now is 07:21 AM.