BASH Environment Variables as arguments?


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions BASH Environment Variables as arguments?
# 1  
Old 02-09-2010
BASH Environment Variables as arguments?

1. The problem statement, all variables and given/known data:
Write a shell program called myenv which takes one argument. The argument should be the name of an environment variable, such as PATH HOME etc. myenv should print out the value of the variable given as the argument. If no argument is given, or the argument is invalid, your program could do unpredictable things!
e.g., myenv PRINTER
should print a line such as: PRINTER=kc3500
e.g., myenv HOME PRINTER
should print a line such as: HOME=/home/dwoit


2. Relevant commands, code, scripts, algorithms:
None that I can think of? Besides the basic start of #!/bin/bash


3. The attempts at a solution (include all code and scripts):
I'm not sure how to write this... I assume I could try setting the argument to a local variable within the script, but that's the next topic (local variables) so I don't think I have to. I know you can see the values of environment variables by typing in $ and then the one you want, so I tried doing ${"$1"} within the program but it doesn't work (I get a bad substitution error) and I have no idea how else to do it. Any help is appreciated, thanks.

#!/bin/bash
echo ${"$1"}
exit 0

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Ryerson University, Toronto, Canada, Denise Woit, CPS393
# 2  
Old 02-11-2010
You have it all in the problem statement: Think well...
Quote:
myenv PRINTER
should print a line such as: PRINTER=kc3500
e.g., myenv HOME PRINTER
should print a line such as: HOME=/home/dwoit
From the examples you must take in account only the 1rst given argument.
But this argument is also a variable, no?
So how will you print out the $1 if you dont store it in a variable ( call it DISPL_ENV) to display?
Why look for complicated syntax for something dead simple if you take the time to read what you are asked.
If you need more just post, but I do hope you find the solution by yourself now
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sourcing .cshrc (C shell) environment variables to bash

I have tried with the following: csh -c 'source ~/.cshrc; exec bash' # works perfectly (cat ~/.cshrc; echo exec bash) | csh # not working And, using sed, I successfully retrieved the environment variables from ~/.cshrc sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc but now... (6 Replies)
Discussion started by: royalibrahim
6 Replies

2. Shell Programming and Scripting

How to use ifdef for bash variables in csh environment?

Hi Guys, I have a a bash script and i am exporting a variable in it. I am calling a csh script from this bash script. The variable "ABC" will be visible in csh script. ks.bash export ABC = abc ./kp.csh ab.csh echo $ABC setenv ABC =cde (i want to assign this value to ABC only if... (4 Replies)
Discussion started by: vdhingra123
4 Replies

3. HP-UX

Environment Variables

Hi Experts, Need your help in understanding the commands to setup the environment variables in hp-ux. Beleive need to use either set,setenv or export. I am confused between above three options, when to use which option? On command line, I have tried both set and setenv but couldn't... (1 Reply)
Discussion started by: sai_2507
1 Replies

4. Shell Programming and Scripting

Environment variables

I have read tons of posts about how you can't set persisting environment variable in a child script of a shell and have it persist. The only way is to source a file as % . <scriptname> I am finding that true... but I know there is a way around it. I just don't know how. I worked for 6... (5 Replies)
Discussion started by: rwa25
5 Replies

5. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

6. UNIX for Dummies Questions & Answers

Environment variables

why are all environment variables represented in a fixed format regardless of the shell you use? like $HOME $PATH etc (6 Replies)
Discussion started by: sravani
6 Replies

7. UNIX for Dummies Questions & Answers

help..Environment variables...

hi, 1). i would like to know what is meant by environment variables? 2). is the number of envi variables is a constant number for unix systems? 3). how to see the list of envi variables (and the values of the envi variables)in a single command? 4). if this questions were already asked... (3 Replies)
Discussion started by: sekar sundaram
3 Replies

8. UNIX for Dummies Questions & Answers

environment variables

Hi Folks, Is it possible somehow to unset all the environment variables which have been defined before in UNIX (Solaris). Thanks, Slava (3 Replies)
Discussion started by: spavlov
3 Replies

9. Programming

environment variables

Hi! How-to get the environment variables in GNU. getenv() only fetches the ones that you can find under export (not the ones under declare)... best regars .David (2 Replies)
Discussion started by: Esaia
2 Replies

10. UNIX for Dummies Questions & Answers

what is the use of Environment variables

what is the actual use of environment variables. I know only PS1, LOGNAME, PS2 variables what are the other variables & what is there use (2 Replies)
Discussion started by: indianguru
2 Replies
Login or Register to Ask a Question