Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
02-08-2007, 04:16 PM
|
#1
|
Member
Registered: Nov 2006
Posts: 188
Rep:
|
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.
|
|
|
02-08-2007, 04:24 PM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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!
|
|
|
02-08-2007, 05:40 PM
|
#3
|
Senior Member
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687
Rep:
|
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.
|
|
|
02-08-2007, 05:53 PM
|
#4
|
Member
Registered: Nov 2006
Posts: 188
Original Poster
Rep:
|
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?
|
|
|
02-08-2007, 06:03 PM
|
#5
|
Moderator
Registered: May 2001
Posts: 29,415
|
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 12:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|