1 Write a program called valid that prints yes or valid if i

1. Write a program called valid that prints “yes or valid” if its argument is a valid shell variable name and “no or invalid” otherwise:

(Hint: Define a regular expression for a valid variable name and then enlist the aid of grep or sed.)

Solution

/* C Program to check whether a string is shell variable or not. This program assumes that shell or environment variable should be of Uppercase letters only.

Variable may start either with underscore or an uppercase alphabet.

variable can contain digits also but should not start with it.

*/

#include <stdio.h>
#include <ctype.h>
int main(int argc, char** argv)

{

if(argc <= 1)

{

printf(\"\ Please Provide your Input:\ \");

return 0;

}

char *shVar = argv[1];
int count;
int valid=1;

if((isalpha(shVar[0]) && isupper(shVar[0])) || \'_\' == shVar[0])
{
count = 0;
while(\'\\0\' != shVar[count])
{
if(!isdigit(shVar[count]) && !isalpha(shVar[count]) && \'_\' != shVar[count])
   {
       valid=0;
   break;
   }
else if(isalpha(shVar[count]) && !isupper(shVar[count]))
   {
       valid=0;
       break;
   }
   count++;
}
}
else
{
valid=0;  
}
if(!valid)
{
printf(\"\ Invalid\ \");
}
else
{
printf(\"\ Valid\ \");
}

return 0;

}

1. Write a program called valid that prints “yes or valid” if its argument is a valid shell variable name and “no or invalid” otherwise: (Hint: Define a regular
1. Write a program called valid that prints “yes or valid” if its argument is a valid shell variable name and “no or invalid” otherwise: (Hint: Define a regular

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site