Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.