linux - 在 RedHat Linux 脚本中使用 cd

标签 linux shell

我有一个 shell 脚本,可以对 HTML 文件进行一些 sed 更改。但是,cd 命令似乎不起作用...尽管我使用 UNIX/Linux 已有多年,但我确信这是一个新手问题。请参阅下面的代码。

但是,如果我取出 for 循环并使其独立(包括 chmod 命令)并将 cd 命令放在一个单独的脚本中,其中一行调用循环脚本,例如:

cd dir
/home/me/dothisscript
cd ../nextdir
/home/me/dothisscript
cd ../nextdir
/home/me/dothisscript
etc... etc...

有效...

下面这个没有。

cd 1950
for i in *.html
do
   sed 's%TITLE%title%g' $i > t
   mv t $i
done
for i in *.html
do
   sed 's% - Removethis.com</title>%</title>%g' $i > t
   mv t $i
done
for i in *.html
do
   sed 's%<title>Removethis.com %<title>%g' $i > t
   mv t $i
done
for i in *.html
do
   sed 's%</title>% - Addthis.com</title>%g' $i > t
   mv t $i
done
exec chmod +x *.html
cd ..
cd 1960
for i in *.html
do
   sed 's%TITLE%title%g' $i > t
   mv t $i
done
for i in *.html
do
   sed 's% - Removethis.com</title>%</title>%g' $i > t
   mv t $i
done
for i in *.html
do
   sed 's%<title>Removethis.com %<title>%g' $i > t
   mv t $i
done
for i in *.html
do
   sed 's%</title>% - Addthis.com</title>%g' $i > t
   mv t $i
done
exec chmod +x *.html
cd ..
cd 1967
etc... etc...

最佳答案

改变

exec chmod +x *.html

只是

chmod +x *.html

exec 用指定的程序替换 shell 进程,因此脚本的其余部分永远不会执行。

关于linux - 在 RedHat Linux 脚本中使用 cd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54120958/

相关文章:

linux - 如何在 bash 脚本中将 select 与 awk 一起使用?

C++ 应用程序构建时间/日期保存

java - ubuntu上设置Java环境变量问题

linux - ntsysv : error reading info for service °8B@h@@

linux - 如何解析 unix shell 脚本中路径的结尾?

shell - 如何使用 awk 打印具有特定模式的开始和结束标记之间的记录?

linux - 从文件 shell 脚本中提取子字符串

linux - amd64 psABI 在哪里?

c - GCC 的 __builtin_expect 在 if else 语句中的优势是什么?

shell - 子进程正常退出,状态为 0