g****g 发帖数: 1828 | 1 i made a simple bash script like this:
#!/bin/bash
# ch.sh
cd ..
when i do ./ch.sh, it still stays in the same directory. only when i do
source ch.sh it works.
anyone knows the reason? how to make it run ./ch.sh? |
E*V 发帖数: 17544 | 2 subshell
【在 g****g 的大作中提到】 : i made a simple bash script like this: : #!/bin/bash : # ch.sh : cd .. : when i do ./ch.sh, it still stays in the same directory. only when i do : source ch.sh it works. : anyone knows the reason? how to make it run ./ch.sh?
|
E*V 发帖数: 17544 | 3 at your prompt
$euv:~/tmp>
input bash
$euv:~/tmp>bash
then input cd /dev/
$euv:~/tmp>cd /dev
$euv:/dev>
then ctrl+D
$euv:/dev> ctrl+D
then you will find that
$euv:~/tmp>
【在 E*V 的大作中提到】 : subshell
|
x******g 发帖数: 3952 | 4 用 'source ./ch.sh', 或者 '. ./ch.sh'.
【在 g****g 的大作中提到】 : i made a simple bash script like this: : #!/bin/bash : # ch.sh : cd .. : when i do ./ch.sh, it still stays in the same directory. only when i do : source ch.sh it works. : anyone knows the reason? how to make it run ./ch.sh?
|
c**b 发帖数: 2999 | 5 你好仔细.我一般就chmod +x ch.sh,然后直接ch.sh了,反正ch.sh应该在当前目录.
【在 g****g 的大作中提到】 : i made a simple bash script like this: : #!/bin/bash : # ch.sh : cd .. : when i do ./ch.sh, it still stays in the same directory. only when i do : source ch.sh it works. : anyone knows the reason? how to make it run ./ch.sh?
|
g****g 发帖数: 1828 | 6 能不能直接 ./ch.sh?
that's what i wanted. i want to put it in /usr/local/bin, to be used as a
command.
【在 x******g 的大作中提到】 : 用 'source ./ch.sh', 或者 '. ./ch.sh'.
|
c**b 发帖数: 2999 | 7 既然是要当命令直接使用,用chmod把属性改成可执行就行了啊.
【在 g****g 的大作中提到】 : 能不能直接 ./ch.sh? : that's what i wanted. i want to put it in /usr/local/bin, to be used as a : command.
|
g****g 发帖数: 1828 | 8 now i understand. it changed the directory, but only within the subshell
that runs the script.
in your example, bash is a subshell of the current shell, say tcsh. whatever
you do in the subshell does not affect the parent shell.
【在 E*V 的大作中提到】 : at your prompt : $euv:~/tmp> : input bash : $euv:~/tmp>bash : then input cd /dev/ : $euv:~/tmp>cd /dev : $euv:/dev> : then ctrl+D : $euv:/dev> ctrl+D : then you will find that
|
g****g 发帖数: 1828 | 9 Shell scripts are run inside a subshell, and each subshell has its own
concept of what the current directory is. The cd succeeds, but as soon as
the subshell exits, the previous current directory is restored.
One way to get around this is to use an alias instead:
alias up='cd ..'
Then in a bash shell, you can do something like:
bash-3.00$ pwd
/home/myhome
bash-3.00$ alias up='cd ..'
bash-3.00$ up
bash-3.00$ pwd
/home
bash-3.00$ up
bash-3.00$ pwd
/
【在 g****g 的大作中提到】 : i made a simple bash script like this: : #!/bin/bash : # ch.sh : cd .. : when i do ./ch.sh, it still stays in the same directory. only when i do : source ch.sh it works. : anyone knows the reason? how to make it run ./ch.sh?
|
g****g 发帖数: 1828 | 10 another way to implement this is:
#!/bin/bash
cd ..
# start another shell and replacing the current
exec /bin/bash
【在 g****g 的大作中提到】 : Shell scripts are run inside a subshell, and each subshell has its own : concept of what the current directory is. The cd succeeds, but as soon as : the subshell exits, the previous current directory is restored. : One way to get around this is to use an alias instead: : alias up='cd ..' : Then in a bash shell, you can do something like: : bash-3.00$ pwd : /home/myhome : bash-3.00$ alias up='cd ..' : bash-3.00$ up
|
g****g 发帖数: 1828 | 11
based on this, i made a script that you can go up any number of directories
easily. for example, up 3 will go up three directories from current dir.
#!/bin/bash
# go up any number of directories directly.
if [[ $# -eq 0 ]]
then
echo "going up one directory"
cd ..
elif [[ $# -eq 1 ]]
then
echo "going up $1 directory"
for (( i=1; i<=$1; i++ ))
do
cd ..
done
else
echo "Usage: $0 "
echo "For example: up will go up one directory."
echo " up 2 will go up two directories."
echo
exit
fi
exec /bin/bash
【在 g****g 的大作中提到】 : another way to implement this is: : #!/bin/bash : cd .. : # start another shell and replacing the current : exec /bin/bash
|
c**b 发帖数: 2999 | 12 为什么if [[ $# -eq 0 ]]要用2个[?
一般我用1个[也可以啊.
directories
【在 g****g 的大作中提到】 : : based on this, i made a script that you can go up any number of directories : easily. for example, up 3 will go up three directories from current dir. : #!/bin/bash : # go up any number of directories directly. : if [[ $# -eq 0 ]] : then : echo "going up one directory" : cd .. : elif [[ $# -eq 1 ]]
|
E*V 发帖数: 17544 | 13 both are right
bash 3.x borrow [[ from ksh
【在 c**b 的大作中提到】 : 为什么if [[ $# -eq 0 ]]要用2个[? : 一般我用1个[也可以啊. : : directories
|