由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Linux版 - A simple Bash question about changing directory
相关主题
不用安装的trash bin?一个简单的bash脚本怎么写?
问个基础问题,关于linux command存放地址求一个脚本
strange behavior about chmod 777 -R新手问个shell的问题
substring 的问题linux tcsh下less的问题
autocompletion in Bash问个关于find 的问题
求助一个bash脚本问题gnome下面如何该一类文件的icon?
linux如何生成可执行文件?Linux 下PHP是否可以取得windows Active Directory
怎么吧一个目录下的所有文件名改成大写?怎么搞个简单的虚拟机?
相关话题的讨论汇总
话题: bash话题: directory话题: subshell话题: up话题: cd
进入Linux版参与讨论
1 (共1页)
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

1 (共1页)
进入Linux版参与讨论
相关主题
怎么搞个简单的虚拟机?autocompletion in Bash
何种软件可以帮助生成文件目录结构图?求助一个bash脚本问题
这个可能是什么问题linux如何生成可执行文件?
Samba4 includes full Active Directory schema怎么吧一个目录下的所有文件名改成大写?
不用安装的trash bin?一个简单的bash脚本怎么写?
问个基础问题,关于linux command存放地址求一个脚本
strange behavior about chmod 777 -R新手问个shell的问题
substring 的问题linux tcsh下less的问题
相关话题的讨论汇总
话题: bash话题: directory话题: subshell话题: up话题: cd