在KSH的命令中是否有相当于bash pushd/popd的内置版本?
对于那些不知道bash中pushd和popd是什么的人,这里是手册页中的描述
pushd [-n] [dir]
pushd [-n] [+n] [-n]
Adds a directory to the top of the directory stack, or rotates
the stack, making the new top of the stack the current working
directory. With no arguments, exchanges the top two directo-
ries and returns 0, unless the directory stack is empty.
popd [-n] [+n] [-n]
Removes entries from the directory stack. With no arguments,
removes the top directory from the stack, and performs a cd to
the new …Run Code Online (Sandbox Code Playgroud) 我有一个KornShell(ksh)脚本,它登录到SQL*Plus并执行脚本.在shell脚本中,我想捕获已执行的SQL语句的状态代码.目前SQL存在错误,我无法通过检查$?来捕获它.如何从sql语句中捕获成功或错误代码并将其传递给shell脚本.
ksh脚本片段:
sqlplus $JDBC_FBUID_U/$JDBC_FBPWD_U@$JDBC_FBDB @${FBC_HOME}/FBCS003.sql ${outputfile}
if [ $? != 0 ]
then
msg_txt="The execution of Sql script /tmp/FBCS003.sql failed. Please investigate."
echo ${msg_txt}
echo ${msg_txt} | mailx -r ${fromemail} -s "FBCB003: The execution of Sql script /tmp/FBCS003.sql failed." ${toemail}
epage -n ${pagerdef} ${pagernum} "FBCB003: ${msg_txt}"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
SQL脚本FBCS003.sql
-- Set SQLPlus variables.
SET NEWPAGE 0
SET WRAP OFF
SET LINESIZE 9999
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET PAGESIZE 0
SET COLSEP …Run Code Online (Sandbox Code Playgroud) 我想使用ksh使用home,end,delete,pageup,pagedown.我的术语是xterm-color.这些键适用于tcsh和zsh,但不适用于ksh(打印波浪号〜)
我找到了这个:
bind '^[[3'=prefix-2
bind '^[[3~'=delete-char-forward
bind '^[[1'=prefix-2
bind '^[[1~'=beginning-of-line
bind '^[[4'=prefix-2
bind '^[[4~'=end-of-line
Run Code Online (Sandbox Code Playgroud)
但是当我设置一个bindkey时,最后一个不再起作用.
如何在ksh中使用.kshrc中的这些键?
谢谢.
我们的ksh环境定义了几个函数.可以使用typeset -fksh命令(或functions别名)列出这些函数的名称.是否可以看到这些功能的定义(即源代码)?
这似乎是一个显而易见的问题,但我已经尝试了所有方式的参数typeset -f,没有运气.
作为一个例子(在Linux上):
$ foo()
> {
> echo foo
> }
$ foo
foo
$ typeset -f foo
foo
$
Run Code Online (Sandbox Code Playgroud)
对于环境中默认定义的某些(但不是全部)其他函数,typeset -f 确实显示源.
更新1:Linux内核2.4.21-32正在发生这种情况
更新2:更新2:Ctrl-V给出"版本M 1993-12-28 n +" - 看起来这是一个相当旧版本,所以可能没有以下Gilles提到的修复
谢谢,史蒂夫
我一直在尝试执行以下无法正常运行的UNIX shell脚本.我正在通过KornShell(ksh)运行它.
echo $?;
if [ $? -ne 0 ]
then
failed $LINENO-2 $5 $6
fi
failed()
{
echo "$0 failed at line number $1";
echo "moving $2 to failed folder"
}
Run Code Online (Sandbox Code Playgroud)
这是错误的说法Syntax error:then unexpected..基本上我必须检查最后执行的ksh脚本的最高/最后一个语句的返回码,如果它不等于零,我必须用给定的参数调用函数失败.我之前尝试过分号,但这也没用.
你能帮忙吗?
Edit1:根据输入改变了代码.仍然存在同样的问题.
ksh ../prescript/Pre_process $1 $2 $3
rc=$?;
if [[ $rc -ne 0 ]];then
echo "failed";
exit 1;
Run Code Online (Sandbox Code Playgroud)
Edit2: 它通过使用双方括号在当时的部分工作.我觉得我使用bash脚本的代码为ksh.我在失败的函数调用中遇到问题.在这个例子中,请让我知道ksh中函数调用的适当方式
我被赋予了一个从txt文件中读取模拟进程的任务,看起来像这样.
ID: 35; Arrival_Time: 0; Total_Exec_Time: 4;
ID: 65; Arrival_Time: 2; Total_Exec_Time: 6;
ID: 10; Arrival_Time: 3; Total_Exec_Time: 3;
ID: 124; Arrival_Time: 5; Total_Exec_Time: 5;
ID: 182; Arrival_Time: 6; Total_Exec_Time: 2;
Run Code Online (Sandbox Code Playgroud)
我必须从(先到先得,最短剩余时间,最短剩余时间,循环q = 2)的选择中完成两个算法.我需要根据我选择的两个算法打印出当时运行的当前时间和进程.我已经成功完成了FCFS.我的下一个方法是关于SRT,除了我对算法背后的逻辑有一些严重的问题.
我目前正在尝试一种迭代方法(在下面发布),它在一定程度上起作用(直到当前时间9),但我觉得这可能只是一个幸运的巧合.
有没有人对这个算法有任何建议,或者其他两个.我已经在这项任务上工作了几天,并决定将我的骄傲吸收并在堆叠上发布.
注意:这是我第一次使用shell脚本,所以我的代码可能有点乱.我仍在努力了解KornShell(ksh).
file="/path/to/file.txt"
IFS=': \;'
i=0
while read -r f1 f2 f3 f4 f5 f6
do
integer id[i]="$f2" #id array
integer at[i]="$f4" #arrival time array
integer et[i]="$f6" #exec time array
integer rt[i]=0 #run time so far
integer current[i]=i
((i++))
done <"$file"
integer curr_index=0
integer currTime=0 …Run Code Online (Sandbox Code Playgroud) 我有一个涉及shell脚本并比较其中的值/变量的项目.我在这里和其他地方看过比较变量,我已经尝试了所有给出的各种例子,但我遇到了一些不像广告那样的东西.操作系统是Solaris10
我创建了以下脚本作为学习经历 -
#!/bin/ksh
stest()
{
if $X = $Y
then echo they're the same
else echo they're notthe same
fi
}
X=a
Y=a
stest
echo completed
Run Code Online (Sandbox Code Playgroud)
我不断得到以下的一些变化 -
使用shell sh或ksh-
#./test.sh
./test.sh[2]: a: not found
completed
Run Code Online (Sandbox Code Playgroud)
使用shell bash-
#./test.sh
./test.sh: line 5: a: command not found
completed
Run Code Online (Sandbox Code Playgroud)
我已经尝试if $X = $Y用括号和双括号括起来,然后我回来了
[a: not found
Run Code Online (Sandbox Code Playgroud)
要么
[[a: not found
Run Code Online (Sandbox Code Playgroud)
如果我将变量X和Y更改为数字"1",我会得到相同的东西 -
./test.sh[2]: 1: not found
Run Code Online (Sandbox Code Playgroud)
我试过用单引号,双引号和反向引号括起来.
任何帮助表示赞赏.
我有点不安地发现以下行为:
bash$ false
bash$ true | echo $?
0
bash$ ksh
ksh$ false
ksh$ true | echo $?
0
ksh$ zsh
zsh$ false
zsh$ true | echo $?
1
Run Code Online (Sandbox Code Playgroud)
直观地说,看起来zsh是正确的,而bash和ksh是错误的. $?是,在每种情况下,应该是1.注意,"最近执行流水线的退出状态" false | echo $?两个也打印0 ksh和bash.是标准的灵活的在这一点上(即行为是不确定的),或者是bash和ksh不符合要求的吗?我也很想知道bash和ksh正在做些什么来解决这个问题.
$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
$ zsh --version
zsh 4.3.11 (i386-apple-darwin11.0)
$ ksh --version
version sh (AT&T Research) 1993-12-28 s+
Run Code Online (Sandbox Code Playgroud) ${var:?}Ash、Dash 或 Bash、Zsh以不同的方式处理此处文档中的无效空参数扩展错误。
下面是experiment.sh使用真正的 POSIX 语法的代码:
#!/usr/bin/env sh
empty_var=
read -r value << EOF
Expected fail here ${empty_var:?}"
EOF
printf '$?=%d\nvalue=%s\n' $? "$value"
Run Code Online (Sandbox Code Playgroud)
下面是使用不同 shell 运行实验的代码:
for sh in ash bash dash ksh zsh; do
printf 'Testing with: %s\n' "$sh"
LC_ALL=C "$sh" ./experiment.sh || :
echo
done
Run Code Online (Sandbox Code Playgroud)
得到的结果:
Testing with: ash
./experiment.sh: 5: empty_var: parameter not set or null
$?=2
value=
Testing with: bash
./experiment.sh: line 5: empty_var: parameter null or not set
Testing with: dash
./experiment.sh: …Run Code Online (Sandbox Code Playgroud) ksh ×10
shell ×7
bash ×3
unix ×3
function ×2
scripting ×2
sh ×2
algorithm ×1
behavior ×1
bind ×1
if-statement ×1
key ×1
linux ×1
openbsd ×1
posix ×1
return-value ×1
scheduling ×1
solaris-10 ×1
sqlplus ×1
zsh ×1