LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
Search this Thread
Old 02-08-2007, 03:16 PM   #1
tekmann33
Member
 
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, 03:24 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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!
 
Old 02-08-2007, 04:40 PM   #3
Junior Hacker
Senior Member
 
Registered: Jan 2005
Location: North America
Distribution: Debian testing Mandriva Ubuntu
Posts: 2,687

Rep: Reputation: 59
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, 04:53 PM   #4
tekmann33
Member
 
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, 05:03 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,369
Blog Entries: 54

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


Reply


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 04:23 PM
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 11:58 AM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 05:24 PM
some bash shell scripting eltongeoff Linux - Newbie 2 10-22-2003 01:10 PM
Bash Shell Scripting Help Tangerine Programming 6 05-06-2003 02:10 PM


All times are GMT -5. The time now is 05:55 AM.

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