我正在查看http://tldp.org/LDP/abs/html/why-shell.html并对此感到震惊:
何时不使用shell脚本
...
- 任务关键型应用程序,您在该应用程序上押注公司的未来
为什么不?
确定给定的ksh调用是否正在运行交互式shell的首选方法是什么?
我在一个ENV文件中有一些命令,我想跳过非交互式ksh调用(例如,在执行shell脚本时).
我见过建议范围从:
if [[ $- = *i* ]]; then
# do interactive stuff
fi
Run Code Online (Sandbox Code Playgroud)
... .kshrc除非使用这个神秘的咒语确定壳是交互式的,否则甚至不采购:
ENVIRON=$HOME/.kshrc export ENVIRON
ENV='${ENVIRON[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}' export ENV
Run Code Online (Sandbox Code Playgroud) KornShell(ksh)曾经有一个非常有用的选项来cd遍历类似的目录结构; 例如,给出以下目录:
/home/sweet/dev/projects/trunk/projecta/app/models /home/andy/dev/projects/trunk/projecta/app/models然后,如果您在/home/sweet...目录中,则可以通过键入更改为andy结构中的等效目录
cd sweet andy
Run Code Online (Sandbox Code Playgroud)
因此,如果ksh看到2个参数,那么它将扫描当前目录路径中的第一个值,将其替换为第二个和cd.有人知道Bash内置了类似的功能吗?或者如果没有,那么让Bash以同样的方式工作的黑客攻击呢?
我正在写一个Expect脚本,但是在处理shell提示符时遇到了麻烦(在Linux上).My Expect脚本生成rlogin并且远程系统正在使用ksh.远程系统上的提示包含当前目录,后跟" > "(空间大于空格).脚本代码段可能是:
send "some command here\r"
expect " > "
Run Code Online (Sandbox Code Playgroud)
这适用于简单的命令,但是当我发送的命令超过终端的宽度时(或更确切地说,ksh认为是终端的宽度),事情开始出错.在这种情况下,ksh交互式命令行的一些奇怪的水平滚动,似乎重写提示并在输出中粘贴额外的">".当然,这会导致Expect脚本在执行命令后输出中出现多个提示时出现混淆和不同步(我的脚本包含多个send/ expect对).
我已经尝试将PS1远程系统更改为更具特色的"提示>",但是出现了类似的问题,这表明我不是解决这个问题的正确方法.
我正在想的可能是帮助脚本能够告诉Expect"我知道此时我已经与远程系统正确同步,因此现在刷新输入缓冲区." 该expect语句具有不丢弃输入缓冲区的-notransfer标志,即使模式匹配,所以我认为我需要与之相反.
是否有任何其他有用的技术可以让远程shell表现得更具可预测性?据我所知,Expect经历了大量的工作,以确保生成的会话似乎与远程系统交互,但我宁愿ksh关闭一些更烦人的交互功能(如水平滚动) .
我有一个需要使用关联数组的脚本.作为ksh的新手,我无法找到ksh支持关联数组的任何地方.当我尝试使用常规数组语法和赋值时,我得到一个错误,索引不能那么大.ksh是否支持关联数组?如果没有,替代解决方案是什么?
需要执行以下操作:$ {array [$ name]} = value,稍后在代码中,我需要读取$ {array [$ name]}的值.每次脚本运行时,我都要存储大约2000个值并从数组中读取.
不幸的是,我不能使用perl,因为遗留模块的范围要包含在脚本中.感谢任何帮助,提示或技巧.
嗨我写了一个小脚本:
#!/usr/bin/ksh
for i in *.DAT
do
awk 'BEGIN{OFS=FS=","}$3~/^353/{$3="353861958962"}{print}' $i >> $i_changed
awk '$3~/^353/' $i_changed >> $i_353
rm -rf $i_changed
done
exit
Run Code Online (Sandbox Code Playgroud)
我测试了它和它的wrking罚款.但是它将输出提供给屏幕我不需要输出到屏幕.我只需要制作最终文件$i_353
这怎么可能?
我有一个jar说test.jar有TestJar作为它的主类,一个shell脚本jar_executor.sh和一个java文件.如果我们传递1作为参数,我的test.jar将返回1,如果我们传递任何其他值,则返回2.我的shell脚本正在执行test.jar,如下所示
#!/usr/bin/ksh
java TestJar 1 -cp test.jar
return_code=$?
exit $return_code
Run Code Online (Sandbox Code Playgroud)
在java文件中,我正在创建一个进程并执行shell脚本,并使用以下代码获取其exitvalue
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(sh jar_executor.sh);
int exitVal = process.waitFor();
System.out.println(exitVal);
Run Code Online (Sandbox Code Playgroud)
这个exitVal变量应该按照我们传递的参数打印1或2,但每次打印255.如果我echo $return_code在shell脚本中使用,那么我得到正确的值.请帮助我为什么我得到255的价值exit.提前致谢!!!
我有一个shell脚本,在第一行中我要求用户输入他们希望脚本运行的分钟数:
#!/usr/bin/ksh
echo "How long do you want the script to run for in minutes?:\c"
read scriptduration
loopcnt=0
interval=1
date2=$(date +%H:%M%S)
(( intervalsec = $interval * 1 ))
totalmin=${1:-$scriptduration}
(( loopmax = ${totalmin} * 60 ))
ofile=/home2/s499929/test.log
echo "$date2 total runtime is $totalmin minutes at 2 sec intervals"
while(( $loopmax > $loopcnt ))
do
date1=$(date +%H:%M:%S)
pid=`/usr/local/bin/lsof | grep 16752 | grep LISTEN |awk '{print $2}'` > /dev/null 2>&1
count=$(netstat -an|grep 16752|grep ESTABLISHED|wc -l| sed "s/ //g")
process=$(ps -ef | …Run Code Online (Sandbox Code Playgroud) 我在shell脚本中遇到问题.我缩小了问题的范围,发现这是因为标准输入的文件描述符不是在proc文件系统中生成的.下面是我编写的测试脚本:
#!/bin/ksh
var=`ls -lrt /proc/$$/path/0`
echo $var
if [[ -f /proc/$$/path/0 ]]
then
echo "found file descriptor"
else
echo "file descriptor not found"
fi
Run Code Online (Sandbox Code Playgroud)
我在/ tmp /目录中使用示例输入文件对此进行了测试:
$ ./checkip.sh < /tmp/testip
lrwxrwxrwx 1 root root 0 Dec 31 09:15 /proc/19358/path/0 -> /tmp/testip
found file descriptor
Run Code Online (Sandbox Code Playgroud)
现在我在我们遇到问题的目录中进行了测试.
$ ./checkip.sh < /var/opt/xxxxxxxx/testip
lrwxrwxrwx 1 root root 0 Dec 31 09:15 /proc/20124/path/0
file descriptor not found
$
Run Code Online (Sandbox Code Playgroud)
我想可能是目录xxxxxxxx.所以我再次使用父目录中的文件对此进行了测试.
$ ./checkip.sh < /var/opt/testip
lrwxrwxrwx 1 root root 0 Dec 31 09:16 /proc/21286/path/0 -> /var/opt/testip …Run Code Online (Sandbox Code Playgroud)