Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 02-08-2007, 04:16 PM   #1
Registered: Nov 2006
Posts: 188

Rep: Reputation: 30
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.
Old 02-08-2007, 04:24 PM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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:
read number
Writing them in sequence does a string concatenation!
Old 02-08-2007, 05:40 PM   #3
Junior Hacker
Senior Member
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 61
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.
Old 02-08-2007, 05:53 PM   #4
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
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?
Old 02-08-2007, 06:03 PM   #5
Registered: May 2001
Posts: 29,415
Blog Entries: 55

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash shell scripting HLM01 Linux - Newbie 7 01-31-2008 05:23 PM
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 12:58 PM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 06:24 PM
some bash shell scripting eltongeoff Linux - Newbie 2 10-22-2003 02:10 PM
Bash Shell Scripting Help Tangerine Programming 6 05-06-2003 03:10 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:44 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration