I can't seem to solve this problem. Please assist. Thanks!
#!/bin/bash
UserDir="$(echo ~$1)"
echo "Changing directory with variables"
cd "$UserDir"
echo "Changing directory without variables"
cd ~pearsn
pwd
Output:
pearsn$ sh -x ./test.bash pearsn
++ echo '~pearsn'
+ UserDir='~pearsn'
+ echo 'Changing directory with variables'
Changing directory with variables
+ cd '~pearsn'
./test.bash: line 6: cd: ~pearsn: No such file or directory
+ echo 'Changing directory without variables'
Changing directory without variables
+ cd /Users/pearsn
+ pwd
/Users/pearsn
The answer is to use "eval" but the reason is not easy to explain. I'll give it a try:
The shell evaluates commandlines on a fixed step-by-step basis. For instance you rely implicitly on this mechanism when you write "var=$(somecommand $othervar)" as you do in your script.
The shell has somehow to follow a recipe to expand "$othervar" prior to "$(...)", because the other way round the command would get a literal "$othervar" as an argument instead of the content of the variable.
For the same reason the following will not work:
We could expect the shell to first expand "$varname" to "var" and then expand "$var" to "something" - but this is not the case because all variables are expanded in the same step and therefore at the same time, not one after the other.
For the same reason your code fails: the step where "~" is expanded to the home directory is already over when you try to evaluate it and therefore you are left with a literal "~pearsn" instead of the home directory of the user.
Still, there is good news: you can restart the evaluation process with step 1 by using the keyword "eval" - in fact this is the reason for the existence of this keyword. Lets start with the variables example from above:
The shell would - because finding nothing else making sense - the "$$" expand to the PID, which is why we will have to protect it with the backslash. So the first time the line is evaluated it goes like this:
(eval)=keyword (echo)=keyword (\$)=a "$" which is meant to remain one ($varname)=oops, we have to expand that one -> replace with "var"
Since the "eval" is there the resulting line is now evaluated again (without the "eval" now):
And as you will notice this works in the expected way.
What does this mean to your problem? Try the following:
I hope this helps.
bakunin
PS: Notice, that the evaluation process is slightly different in ksh and in bash. Your script would have worked in Korn shell, but fails in bash, because the steps are laid out a little differently.
#!/bin/bash
X=$(</home/cogiz/computerhand.txt) # (3S 8C 2H 6D QC 8S 4H 5H)
Y=$(</home/cogiz/topcardinplay.txt) # KS
A=( "${Y::1}" )
B=( "${Y:1}" )
for e in ${X}; do
if ]; then # searching for valid cards K,S or 8
... (0 Replies)
i'm trying to write a script that has 2 variables, and uses the 1st variable as a number and the 2nd a name to create directories. so if you typed in ./myscript 5 week, it would create 5 directories named week1 - week5. whenever i run this, i get an error message saying week5 already exists, so i... (3 Replies)
I would like to extract directories from a specific place and keep them into an array of variables to run functions into it. Example,
bash-3.00$ls
adrian bryan caren derrick
I want to keep each directory names into a variable
adrian --> document
bryan --> document
caren --> document... (3 Replies)
How can i create a file, for example with a touch command, in a different directory from the current one i am in, in one single line command? (1 Reply)
I'm having a spot of trouble. I'm trying to test three variables for a NULL value in a bash shell script. If a null value is detected in the variable to set it to set the variable to a default value.
here is what I have:
testResponseA=3
testResponseB=
testResponseC=4
for test in... (5 Replies)
In FORMAT->ANALYZE->SETUP there's a couple variables you can set for the various functions. However, everytime I exit format it reverts back to the defaults. Is there a file I can edit somewhere to change these default settings? (0 Replies)
Hi All
I need to create a script which would change Permissions to 775
All the Files and directories will be mentioned in the Paramter files
Can anyone give a Hint how to proceed in this ??
THanks (1 Reply)
hi friends,
i'm new to shell scripting,can i know how to change the environment variables without altering anythng in .bash_profile as the change in it is for a specific user but i want the change to be available to every user who logs in.
bye. (1 Reply)
Hi all,
can some one help me in chmod command, and let me know the various combinations for this command.
for : eg chmod -R 777 <dir names>
this gives all rights to all but i want the specific access levels kindly help me out in this issue.
Thank you,
lakshmanan (2 Replies)
I just want to exit my script in a new directory from a bash shell. Problem is that the script internally changes to the directory I want to move to, however when exits is still in the original directory. Does that make sense?
ie usage: goto null
changing from /usr/bin/xtra/test/test3/
... (8 Replies)