Need a script to check if an argument is valid shell variable

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a script to check if an argument is valid shell variable
# 1  
Old 02-12-2014
Need a script to check if an argument is valid shell variable

I need a script that should print 'yes' if the argument is a valid shell variable name else 'No' if it is not a valid shell variable. A valid one begins with an alphabet or percentage (%) character and is followed by zero or more alphanumberic or percentage (%) characters.

For example:

$ 369
$ abs_lo
$ %london


Last edited by Scrutinizer; 02-12-2014 at 11:53 AM.. Reason: code tags
# 2  
Old 02-12-2014
You do realise we are not here to do the work for you but to help you do your work!
Not quite the same thing, so what cant you do or what have you done so far?
# 3  
Old 02-12-2014
This is what I have written but I am not sure if it is correct as I am new to UNIX
FirstChar = cut -c1 $1
if [FirstChar -eq A-z] || [FirstChar -eq a-z]|| [FirstChar -eq '%']
    Echo "yes"
    Echo "no"

Let me know if it is correct.


Last edited by vbe; 02-12-2014 at 12:08 PM.. Reason: code tags...
# 4  
Old 02-12-2014
1) a test like [FirstChar -eq A-z] needs:
space between the square brackets: [ FirstChar -eq A-z ]
then -eq is for numeric values (Integers)
# 5  
Old 02-12-2014
ant:/home/vbe $ 0002 tata
ant:/home/vbe $ 0002 0titi
ant:/home/vbe $ 0002 %toto
ant:/home/vbe $ 0002 1gaga

# 6  
Old 02-12-2014
So a possible solution or a base to start to work on:
echo STR=$STR
Firstchar=$(echo $STR|cut -c1)
echo Firstchar=$Firstchar

if [ "$Firstchar" = "%"  ]
   echo YES
   let I=$Firstchar+1 2>>/dev/null
   if [ $I -ge 1 ]   #or [ $I -gt 0 ]
      echo NO
      echo YES

# 7  
Old 02-13-2014
Im impressed by the little reactiveness of thread owner.. wanted to show him the correct syntax but also this approach would be laborious and point him to some readings like:
Case statement in shell

Because here obviously its case statement that should be used...
Oh well...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check for valid hostnames

Hello, I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki : Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "" is a hostname. Each label... (8 Replies)
Discussion started by: rahul2662
8 Replies

2. Shell Programming and Scripting

Check if time format is valid

How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid. echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }' ex: ./script.ksh 12:34:21 = okay ./script.ksh... (10 Replies)
Discussion started by: erin00
10 Replies

3. Shell Programming and Scripting

Passing variable as an argument to another script

Hi, I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem. varaiable "a" value should be -b "FPT MAIN". When we pass "a" to another script, we are expecing it to get substitue as ./ -b "FPT MAIN". But, it is getting... (9 Replies)
Discussion started by: Manasa Pradeep
9 Replies

4. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

5. Shell Programming and Scripting

Shell script that check the argument passed to it and prints error if test condition is not met

I want to make a script that check for the argument passed to it and generates an error in case any character/string argument passed to it. I am using below code, but its not working. can anyone help. #!/bin/bash if ]; then echo 'An integer argument is passed to the script hence... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

6. Shell Programming and Scripting

how to check for valid password

I need to check if an account has a valid password. Would something like this work? read ACCNAME if grep -q "^$ACCNAME:\$6:" /etc/shadow; thenI noticed every entry in my shadow file that has a password starts with $6 ... it works for my current setup, but would it always work? I can't test... (4 Replies)
Discussion started by: ADay2Long
4 Replies

7. Homework & Coursework Questions

Bash shell - Check if value is valid directory.

1. The problem statement, all variables and given/known data: The script usage will be as follows: library.third source_directory - Your script will display an appropriate error message and exit with status 3 if no parameters are given - Your script will display an appropriate error... (2 Replies)
Discussion started by: netmaster
2 Replies

8. Shell Programming and Scripting

how to assign script argument to a variable

I have a script file. I am running it by command sh 10102004 where 10102004 is the script argument. I wan to assign this 10102004 to a variable. How can i do this? I tried &1 and awks ARGV its not working :( (2 Replies)
Discussion started by: rohankit
2 Replies

9. Shell Programming and Scripting

Checking the valid paths in the shell script

Hi i am using a shell script for renaming the files and placing in the remote location(FTP) my code is: cd $1 day=$(date +%d) for i in `ls -1 BBW*` do last=`tail -1 $i` pat=`expr "$last" : '.*(\(.*\)).*'` pat=`echo $pat | sed 's/ /_/'` pat=$pat$day mv $i $pat rm -f $day done... (3 Replies)
Discussion started by: srivsn
3 Replies
Login or Register to Ask a Question