function去呼叫functio [論壇 - Ubuntu 程式設計]


正在瀏覽:   1 名遊客


 到底部   前一個主題   下一個主題  [無發表權] 請登錄或者註冊



function去呼叫functio
會員二級
註冊日期:
2015/11/16 9:07
所屬群組:
已註冊使用者
等級: 6
HP : 0 / 137
MP : 19 / 3154
EXP: 48
離線
您好:
請問 .sh 若用 function去呼叫function ,
1.回傳值 是否依定要 數字
2.回傳值 要如何拿來判斷?
3.
whattoday()
{
date +%A
}

這樣算回傳嗎?

4.以下程式碼都無法常RUN

#!/bin/bash

AA()
{
return 9
}

BB()
{

xx=`AA`
if [ $(xx) -eq 9 ]; then
return 0
else
return 1
fi

}


if [ ! $(BB) ]; then
echo "Y"
else
echo "N"
fi


謝謝!

2016/3/22 15:41
應用擴展 工具箱
回覆: function去呼叫functio
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30282
EXP: 23
離線
wayout 寫到:
您好:
請問 .sh 若用 function去呼叫function ,
1.回傳值 是否依定要 數字
2.回傳值 要如何拿來判斷?
3.
whattoday()
{
date +%A
}

這樣算回傳嗎?

4.以下程式碼都無法常RUN

#!/bin/bash

AA()
{
return 9
}

BB()
{

xx=`AA`
if [ $(xx) -eq 9 ]; then
return 0
else
return 1
fi

}


if [ ! $(BB) ]; then
echo "Y"
else
echo "N"
fi


謝謝!



先提供一個最近看到的一個小手冊「bash-handbook / Positional parameters」,
很容易閱讀,可以快速的吸收一些重點概念,
,我也還在研讀,還不熟 bash script programing

Orz...

2016/3/22 18:43
應用擴展 工具箱
回覆: function去呼叫functio
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30282
EXP: 23
離線
備份連結

=====================================================

wayout 寫到:
#!/bin/bash

AA()
{
return 9
}

BB()
{

xx=`AA`
if [ $(xx) -eq 9 ]; then
return 0
else
return 1
fi

}


if [ ! $(BB) ]; then
echo "Y"
else
echo "N"
fi


謝謝!


改成


#!/usr/bin/env bash

AA()
{
return 9
}

BB()
{

if [ AA -eq 9 ]; then
return 0
else
return 1
fi

}


if [ !BB ]; then
echo "Y"
else
echo "N"
fi




執行


$ help if



可以看到 help


if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
Execute commands based on conditional.

The `if COMMANDS' list is executed. If its exit status is zero, then the
`then COMMANDS' list is executed. Otherwise, each `elif COMMANDS' list is
executed in turn, and if its exit status is zero, the corresponding
`then COMMANDS' list is executed and the if command completes. Otherwise,
the `else COMMANDS' list is executed, if present. The exit status of the
entire construct is the exit status of the last command executed, or zero
if no condition tested true.

Exit Status:
Returns the status of the last command executed.



也可以執行


$ help test



觀看相關的help

2016/3/22 18:51
應用擴展 工具箱
回覆: function去呼叫functio
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30282
EXP: 23
離線
備份連結

================================

範例一


#!/usr/bin/env bash

echo $(date +%A)



範例二


#!/usr/bin/env bash

NOW=$(date +%A)

echo $NOW



範例三


#!/usr/bin/env bash

echo `date +%A`




範例四


#!/usr/bin/env bash

NOW=`date +%A`

echo $NOW



範例五


#!/usr/bin/env bash

THE_CMD='date +%A'

echo $THE_CMD




範例六


#!/usr/bin/env bash

THE_CMD='date +%A'

$THE_CMD



更多請參考「man bash」,找尋「Command Substitution」


可以看到下面一段,還有其他的


...略...

Command Substitution
Command substitution allows the output of a command to replace the
command name. There are two forms:

$(command)
or
`command`

...略...


2016/3/22 19:03
應用擴展 工具箱
回覆: function去呼叫functio
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 37
HP : 0 / 905
MP : 679 / 30282
EXP: 23
離線
備份連結

===============================================

範例一


#!/usr/bin/env bash

AA()
{
return 9
}

AA

echo $?




範例二


#!/usr/bin/env bash

AA()
{
return 9
}

echo $(AA)



這裡會得到空白值,不是原本預期的return,要用上面的「$?」

範例三


#!/usr/bin/env bash

AA()
{
echo 'abc'
}

echo $(AA)



範例四


#!/usr/bin/env bash

AA()
{
echo 'abc'
}

RS=$(AA)

echo $RS




範例五


#!/usr/bin/env bash

AA()
{
echo 'abc'
}

RS=$(AA)

$RS



這裡會得到「test.sh: line 10: abc: command not found」,也就是最後一行「$RS」,是在執行「abc」,直接下指令「abc」就會得到「abc: command not found」。


範例六


#!/usr/bin/env bash

whattoday()
{
date +%A
}


NOW=$(whattoday)

echo $NOW



除了剛剛提到的「Command Substitution」,

更多請參考「man bash」,找尋「EXIT STATUS」。


可以找到下面一段,和其他的


EXIT STATUS

The exit status of an executed command is the value returned by the
waitpid system call or equivalent function. Exit statuses fall between
0 and 255, though, as explained below, the shell may use values above
125 specially. Exit statuses from shell builtins and compound commands
are also limited to this range. Under certain circumstances, the shell
will use special values to indicate specific failure modes.

2016/3/22 19:26
應用擴展 工具箱
回覆: function去呼叫functio
會員二級
註冊日期:
2015/11/16 9:07
所屬群組:
已註冊使用者
等級: 6
HP : 0 / 137
MP : 19 / 3154
EXP: 48
離線
謝謝,我再試試看!

2016/3/23 13:01
應用擴展 工具箱


 [無發表權] 請登錄或者註冊


可以查看帖子.
不可發帖.
不可回覆.
不可編輯自己的帖子.
不可刪除自己的帖子.
不可發起投票調查.
不可在投票調查中投票.
不可上傳附件.
不可不經審核直接發帖.