tree.ksh

#!/bin/bash
# tree.sh

# Written by Rick Boivie. Used with permission.
# This is a revised and simplified version of a script
# by Jordi Sanfeliu (and patched by Ian Kjos).
# This script replaces the earlier version used in
# previous releases of the Advanced Bash Scripting
# Guide.

# ==> Comments added by the author of this document.


search () {
for dir in `echo *`
# ==> `echo *` lists all the files in current working directory,
#+ ==> without line breaks.
# ==> Similar effect to for dir in *
# ==> but "dir in `echo *`" will not handle filenames with blanks.
do
if [ -d "$dir" ] ; then # ==> If it is a directory (-d)...
zz=0 # ==> Temp variable, keeping track of directory level.
while [ $zz != $1 ] # Keep track of inner nested loop.
do
echo -n "| " # ==> Display vertical connector symbol,
# ==> with 2 spaces & no line feed in order to indent.
zz=`expr $zz + 1` # ==> Increment zz.
done

if [ -L "$dir" ] ; then # ==> If directory is a symbolic link...
echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'`
# ==> Display horiz. connector and list directory name, but...
# ==> delete date/time part of long listing.
else
echo "+---$dir" # ==> Display horizontal connector symbol...
# ==> and print directory name.
numdirs=`expr $numdirs + 1` # ==> Increment directory count.
if cd "$dir" ; then # ==> If can move to subdirectory...
search `expr $1 + 1` # with recursion ;-)
# ==> Function calls itself.
cd ..
fi
fi
else

zz=0 # ==> Temp variable, keeping track of directory level.
while [ $zz != $1 ] # Keep track of inner nested loop.
do
echo -n "| " # ==> Display vertical connector symbol,
# ==> with 2 spaces & no line feed in order to indent.
zz=`expr $zz + 1` # ==> Increment zz.
done

echo `pwd`/$dir

fi
done
}

if [ $# != 0 ] ; then
cd $1 # move to indicated directory.
#else # stay in current directory
fi

echo "Initial directory = `pwd`"
numdirs=0

search 0
echo "Total directories = $numdirs"

exit 0
<think>好的,我需要帮助用户解决如何执行或调用名为aaa.kshksh脚本文件。首先,用户提到“source”这个命令,通常在Unix/Linux系统中,source或者点命令(.)用来在当前shell环境中执行脚本,而不是启动一个新的子shell。因此,正确的做法应该是使用`. aaa.ksh`或者`source aaa.ksh`。 接下来,我需要确认用户是否有执行权限。如果用户没有权限,可能需要使用`chmod +x aaa.ksh`来添加执行权限。不过,当使用source命令时,脚本不需要有执行权限,因为它是由当前的shell解释器读取的,而不是作为可执行文件运行。这点可能需要进一步澄清。 然后,路径的问题。如果aaa.ksh不在当前目录下,用户需要指定完整的路径,比如`source /path/to/aaa.ksh`或者使用相对路径,比如`source ./aaa.ksh`。如果脚本在当前目录,前面的./是必须的,否则shell可能找不到该文件。 此外,用户可能遇到的常见错误包括脚本中的语法错误,或者环境变量的问题。比如,脚本是否以正确的shebang行开头,如`#!/bin/ksh`,或者是否在脚本中正确设置了变量。如果脚本中有错误,source命令会报错,并停止执行。 参考用户提供的引用内容,特别是引用[2],用户之前处理过.nmon文件,可能对shell脚本有一定了解。但需要确保他们了解source和直接执行的区别。比如,直接执行脚本会启动子shell,而source则在当前shell中运行,因此变量的更改会影响当前环境。 最后,生成相关问题需要考虑用户可能的后续疑问,比如权限问题、路径问题、调试脚本的方法以及source与其他执行方式的区别。需要确保这些问题能帮助用户深入理解如何正确执行ksh脚本。</think>[用户可见层] ### 如何调用名为 `aaa.ksh` 的 KSH 脚本文件? #### 步骤说明: 1. **确保脚本可执行权限** 使用 `chmod` 命令添加执行权限(如果尚未设置): ```bash chmod +x aaa.ksh ``` 如果仅需在当前会话中运行(无需执行权限),可直接通过 `source` 或 `.` 调用[^1]。 2. **通过 `source` 或 `.` 执行脚本** 在终端中输入以下命令(两者等效): ```bash source aaa.ksh # 或 . aaa.ksh ``` **作用**:在当前 Shell 环境中执行脚本,脚本中定义的变量或函数会保留在当前会话中[^2]。 3. **指定脚本路径(若不在当前目录)** 如果脚本不在当前目录,需提供完整路径或相对路径: ```bash source /path/to/aaa.ksh # 或 . ./subdir/aaa.ksh ``` #### 常见问题排查: - **权限问题**:若提示 `Permission denied`,需执行 `chmod +x aaa.ksh`。 - **路径错误**:若提示 `No such file or directory`,检查路径是否正确。 - **脚本语法错误**:若脚本内容有误,错误信息会直接显示在终端。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值