我的职能是:
#execute vim and then exit on err return
ve() {
vim "$@" || exit
}
export -f ve
#compile c source -> asm source in new tab in xfce terminal
casm() {
[ -z ${1} ] && return
fname=$1
xfce4-terminal --tab -e "bash -c "\""gcc -S $fname.c; ve $fname.s ;
如何在Bash脚本中处理内部函数的返回值(该函数可能返回非零值),而不被陷阱捕获。
例如,如果perform_test 返回1,脚本就结束了,因为非零返回值会被exit_handler函数的调用捕获和处理。
我怎样才能避免这种行为?
谢谢
在这里,脚本:
#!/bin/bash
set -o pipefail # trace ERR through pipes
set -o errtrace # trace ERR through 'time command' and other functions
set -o nounset ## set -u : exit th
我想生成一些exit代码,比如说20。但是如果在shell上这样做的话:
$ exit 20
外壳是关闭的(大小写)或我被注销(文本控制台大小写)。当然,在.sh脚本中,这个命令工作得很好。
我已经测试了,但没有成功:
$ return 20
bash: return: can only `return' from a function or sourced script
$ break 20
bash: break: only meaningful in a `for', `while', or `until' loop
是否有任何方法可以将值assign为
因此,您需要执行标准的getopts设置:
while getopts :a:bc option
do
case "${option}"
in
a)
# do a manditory thing!
;;
b)
# do a optional thing!
;;
c)
# for real, you usually would set variables using getopts
;;
我在很多bash脚本中都使用了一个简单的循环,但是这个特定的循环似乎不起作用。
#!/bin/bash
function main
{
echo here
while getopts "Ah" cli_opt; do
case ${cli_opt} in
A)
echo "op A"
start_all
return $?
;;
*)
echo invalid option
校正后
@stribizhev是对的。你不能在bash regex中使用外观。我用grep做的。
#!/bin/bash
fqdn=$1
result=`echo $fqdn | grep -P '(?=^.{1,254}$)(^(?>(?!\d+\.)[a-zA-Z0-9_\-]{1,63}\.?)+(?:[a-zA-Z]{2,})$)'`
if [[ -z "$result" ]]
then
echo "$fqdn is NOT a FQDN"
else
echo "$fqdn is a FQDN"
f
我目前正在编写一个BASH脚本,我希望在其中同时使用位置参数和可选参数。位置参数必须发生在指定的位置(例如:$1,$2),而可选参数可以发生在其命令行标志所表示的任何位置。这是我的剧本:
#!/usr/bin/env bash
usage() {
cat << EOF
Usage: progam ACTION NAME -k KEY_NAME
ACTION ...... The program action to initiate
NAME ........ The name of the object to create
KEY_NAME .... Th
我已经编写了一个小的库函数来帮助我在脚本所有者不是root用户时退出:
#!/bin/bash
# Exit for non-root user
exit_if_not_root() {
if [ "$(id -u)" == "0" ]; then return; fi
if [ -n "$1" ];
then
printf "%s" "$1"
else
printf "
我有一个bash脚本,它的函数需要与不同的参数并行运行。我需要知道至少有一次执行失败(返回非零)--不管失败了多少次。
该命令接受执行的参数数组。由于负载较高,我需要将并发限制为4次并发运行。我还需要在父进程(运行bash脚本的进程)中打印日志。
这是我正在运行的函数:
function run_and_retry {
EXIT_STATUS=0
$COMMAND || EXIT_STATUS=$?
if [ $EXIT_STATUS -ne 0 ]; then
EXIT_STATUS=0
$COMMAND || EXIT_STATUS=$?
fi
r
澄清的Edited
我有一个通常应该执行的脚本。但是,出于测试目的,我希望能够获得它的源代码,以获得所有函数和变量的定义,并根据需要设置环境并创建目录,然后在肉类部分开始之前返回。<#>Basically我需要获得一个与环境交互的shell,因为它负责执行一些检查,但前提是所有检查都通过。
代码流如下所示:
#!/bin/bash
SOME_VAR="hey"
checker() {
# can find fatal conditions which should exit the script
# or stop sourcing it
# ...
我想编写一个bash脚本,它模拟Korn命令:cd old new,它在PWD中用new代替old,然后尝试将cd转到新目录。
下面是脚本:
#!/usr/bin/env bash
function korn_cd
{
case "$#" in
0 | 1 ) builtin cd $1 ;;
2 ) if ! echo "$PWD" | grep "$old" ; then
echo "bash: cd: bad subsitiution";
是否可以在bash脚本文件中定义自己的退出函数。
示例:
$] vi myBash.sh
#!/usr/bash
function myFunc()
{
echo "Inside myFunc"
}
function exit()
{
echo "My Own Exit Called"
exit 0
}
myFunc
exit 0
$] sh myBash.sh
Inside myFunc
My Own Exit Called
$]
我今天刚开始在bash编程有件事我搞不懂.下面是我写的剧本:
#!/bin/bash
FILE=test.txt
test_file () {
if [ -e "$FILE" ]; then
echo "$FILE exists"
else
echo "$FILE does not exist"
return 1
fi
return
}
$(test_file)
当我运行脚本(命名测试)时,它输出以下错误消息:
"./test: line 16: test.txt: command n
我想编写一个bash脚本来检查输入消息是否采用以下格式。“类别:解释”。只有当类别是bash、java、python中的一个时,它才会返回成功。Bash不是我的语言,所以请原谅我这个丑陋的代码。 #!/bin/sh
input=$1
if[["$input"==bash:*]]
then exit 0
fi
if[["$input"==java:*]]
then exit 0
fi
if[["$input"==python:*]]
then exit 0
fi
echo "categories are python, bash,
我正在尝试连接到托管在服务器上的oracle db这是我的脚本
#!/bin/bash
RAW=`cat sqlTemp.sql`
IFS=";"
exit=" exit;"
for var in $RAW
do
query=$(echo $var | sed '/^$/d')
sqlplus64 username/pwd@hostname:portnumber/servicename $query 2>> errorLog.txt
ERRORCODE=$?
if [ $ERRORCODE != 0 ]
then
ech