简体   繁体   English

在shell脚本中更改shell

[英]Changing shell inside a shell script

in the default shell the for loop given below 在默认外壳中,下面给出的for循环

for ((i=$llimit; i<=$ulimit; i++)); 
do 
  echo $i
done;

it throws error "'((' is not expected" 它将引发错误“'(('不是预期的”

but when switching to the bash shell 但是当切换到bash shell时

the for loop works fine for循环工作正常

is there a way to change shell inside a shellscript 有没有办法改变shellscript中的shell

or any other solution as this for loop is inside a shell script 或任何其他解决方案,因为此for循环位于shell脚本中

EDIT: 编辑:

this is hte shell script 这是外壳脚本

#!/bin/bash
nav_var=`sqlplus -s tcs384160/tcs#1234 <<\EOF
set pagesize 0 feedback off verify off heading off echo off
select max(sequence#) from v$archived_log where applied='YES' and thread#=2 and        dest_id=2;
exit;
EOF`
echo $nav_var;
ulimit=`expr $nav_var - 30`;
llimit=`expr $ulimit - 200`;
for ((i=$llimit; i<=$ulimit; i++));
do ls -l arch_aceprod_2_${i}_743034701.arc;
done;

The C-style for loop you've used is a bashism . 您使用的C风格的for循环是bashism

Change the line 换线

for ((i=$llimit; i<=$ulimit; i++));

to

for i in $(seq $llimit $ulimit);

and it would work well with both sh and bash . 它可以和shbash


EDIT: If you don't have seq , you could change the loop as: 编辑:如果您没有seq ,则可以将循环更改为:

i=$llimit
while [ $i -le $ulimit ]; do
  echo "Do something here"
  let i=i+1
done

By "default shell" I assume you mean /bin/sh? 通过“默认外壳”,我假设您是指/ bin / sh? Is there a line starting "#!" 是否有以“#!”开头的行? at the top of the script? 在脚本顶部?

Bash is pretty much backwards compatible with sh. Bash与sh几乎向后兼容。 If you put "#!/bin/bash" (without the quotes) as the first line this should get the whole thing to run under bash. 如果您将“#!/ bin / bash”(不带引号)作为第一行,则应使整个内容在bash下运行。

try another for loop syntax 尝试另一个for循环语法

for counter in {$llimit..$ulimit}
do

your logic

done

this works for all type of shells. 这适用于所有类型的外壳。

Or #!bin/bash will also work in your case 或#!bin / bash也适用于您的情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM