Shell编程中如何获取环境变量中设置的路径

TechnoFantasy 2002-06-17 11:56:04
我需要通过Shell编程将一个tar包解压缩到tomcat的根目录下,所以需要在shell中得到环境变量(.profile)中的参数 TOMCAT_HOME 的值,然后将tar包解压缩到这个目录下面的 webapps/ROOT/ 中,应该如何实现?
...全文
1115 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
idlest 2002-06-17
  • 打赏
  • 举报
回复
有趣
nicholaz 2002-06-17
  • 打赏
  • 举报
回复
你的要求可以实现吗?关注。
ajiefudan 2002-06-17
  • 打赏
  • 举报
回复
这4颗星跟linux无关?。。。。
如果是某个用户的profile,你su - 用户名,就可以使用profile中变量
否则的话,自己定义这个变量好了
waterstream 2002-06-17
  • 打赏
  • 举报
回复
谢谢你!
waterstream 2002-06-17
  • 打赏
  • 举报
回复
小弟的功底不深sorry。

-d 是用来检查路径的!

我是一下-z好么?
少等
ajiefudan 2002-06-17
  • 打赏
  • 举报
回复
给你看看man
-a file
True if file exists.
-b file
True if file exists and is a block special file.
-c file
True if file exists and is a character special
file.
-d file
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and is set-group-id.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its ``sticky'' bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than
zero.
-t fd True if file descriptor fd is open and refers to a
terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-O file
True if file exists and is owned by the effective
user id.
-G file
True if file exists and is owned by the effective
group id.
-L file
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N file
True if file exists and has been modified since it
was last read.
file1 -nt file2
True if file1 is newer (according to modification
date) than file2.
file1 -ot file2
True if file1 is older than file2.
file1 -ef file2
True if file1 and file2 have the same device and
inode numbers.
-o optname
True if shell option optname is enabled. See the
list of options under the description of the -o
option to the set builtin below.
-z string
True if the length of string is zero.
-n string
string True if the length of string is non-zero.
string1 == string2
True if the strings are equal. = may be used in
place of ==.
string1 != string2
True if the strings are not equal.
string1 < string2
True if string1 sorts before string2 lexicographi?
cally in the current locale.
string1 > string2
True if string1 sorts after string2 lexicographi?
cally in the current locale.
arg1 OP arg2
OP is one of -eq, -ne, -lt, -le, -gt, or -ge.
These arithmetic binary operators return true if
arg1 is equal to, not equal to, less than, less
than or equal to, greater than, or greater than or
equal to arg2, respectively. Arg1 and arg2 may be
positive or negative integers.
waterstream 2002-06-17
  • 打赏
  • 举报
回复
我给敲错了。

是$TMPPATH 不是$$TMPPATH
ajiefudan 2002-06-17
  • 打赏
  • 举报
回复
-z 是检查字符串是否为空
检查文件为空用 -s来判断
waterstream 2002-06-17
  • 打赏
  • 举报
回复
对不起,我给弄错了,
我刚试了一下,应是:

if[ ! -d "$$TMPPATH" ] ;

if[ ! -d "$$TMPPATH" ]
waterstream 2002-06-17
  • 打赏
  • 举报
回复
-z 是 zero即为空的意思,它判断一个文件是否为空!
nicholaz 2002-06-17
  • 打赏
  • 举报
回复
不会啊,我这可以的,也是用的Bourne Shell
waterstream 2002-06-17
  • 打赏
  • 举报
回复
不知为不知,是为知也!!
ajiefudan 2002-06-17
  • 打赏
  • 举报
回复
具体的要根据shell来区别,可以man 一下
waterstream 2002-06-17
  • 打赏
  • 举报
回复
我用的是Bourne Shell
waterstream 2002-06-17
  • 打赏
  • 举报
回复
楼上的;
if [ $TOMCAT_HOME ];
这条语句好像有点问题?!

if[ ! -d $TOMCAT_HOME ]
这样才正确吧!
ajiefudan 2002-06-17
  • 打赏
  • 举报
回复
if [ -z $TMPPATH ] ; then
echo "no tmppath"
else
...

fi
nicholaz 2002-06-17
  • 打赏
  • 举报
回复
在Shell中判断获得这个环境变量存在否;
#!/bin/bash
if [ $TOMCAT_HOME ];
then
echo "not found"
else
tar xvf /home/a.tar
fi
nicholaz 2002-06-17
  • 打赏
  • 举报
回复
你是对的,刚才是我理解错了,呵呵~
TechnoFantasy 2002-06-17
  • 打赏
  • 举报
回复
不是,可能没讲清楚,我需要在Shell中判断获得这个环境变量存在不,如果不存在则提示,所以不能直接进入然后 tar 解压缩,现在的问题是Shell中是否有语句可以判断某个环境变量是否存在以及获取某个环境变量的值。
ajiefudan 2002-06-17
  • 打赏
  • 举报
回复
不可以吗?
是不是我理解错误?
用户 ajie
在.bash_profiel中export TMPPATH=/home/ajie/tmp
a.tar 在/home/ajie下
写a.sh如下
#!/bin/bash
echo $TMPPATH
cd $TMPPATH
mkdir webapps
cd webapps
mkdir ROOT
cd ROOT
tar xvf /home/ajie/a.tar
有错误吗?

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧