當前位置:首頁 » 編程軟體 » shell腳本加減

shell腳本加減

發布時間: 2024-05-30 21:17:32

linux鍔犲噺涔橀櫎鍛戒護linux鍔犲噺

linux涓嬫湁娌℃湁瀵逛竴鍒楁暟瀛楄繘琛屾眰鍜岀殑鍛戒護錛

瀵圭涓鍒楁暟瀛楁眰鍜岋細awk'{a+=$1}END{printa}'

瀵圭浜屽垪鏁板瓧奼傚拰錛歛wk'{a+=$2}END{printa}'

閫氳繃綆¢亾閫佽繃鍘伙細echoXXX|awk

鎴栬卆wk璇誨彇鏂囦歡

linux1鍒100鐨勫拰鏄澶氬皯錛

1鍒100鐨勫拰鏄5050銆傚傛灉瑕佸幓璁$畻鍑1鍒100鐩稿姞寰楀嚭鐨勫拰鏄澶氬皯錛熼噰鍙栦互涓嬫柟娉曪細鑻ヨ佹眰榪欎竴棰橈紝寰楀垵1鑷100鐩稿姞鐨勫緱鏁幫紝鍙浠ュ厛灝1鍒100澶村熬鐨勬暟瀛楃浉鍔狅紝1+100絳変簬101錛屼箣鍚庡幓灝100梅2絳変簬50錛屾渶鍚庢妸101涔50錛屽彲寰楀嚭寰楁暟涓5050錛屾墍浠1鍒100鐨勫拰鏄5050

Linux緗戠粶鎿嶄綔緋葷粺鐨勯棶棰橈紝涓錛岀紪鍐檚hell鑴氭湰瀹炵幇鍔犲噺涔橀櫎榪愮畻銆傚疄鐜版柟娉曚笉闄愶紝浣嗗敖鍙鑳藉噺灝戝嚭閿欐満鐜囷紵

setd=0;

if

then

echo$3

d=$(($1-$2))

fi

echo$d

linux甯哥敤鍛戒護鏈夊摢浜涳紵

Linux鏄鎴戜滑寮鍙戜漢鍛樺繀涓嶅彲灝戠殑緋葷粺錛屼篃鏄緇忓父鎺ヨЕ鍒扮殑銆傜劧鑰岋紝Linux鍛戒護姣旇緝澶氾紝鏈変簺涓嶅父鐢ㄤ篃闅捐頒綇銆傞偅涔堬紝鎴戜滑濡備綍鏇撮珮鏁堢殑浣跨敤Linux鍛戒護錛岃屽張涓嶅繀鍏ㄩ潰鍦板︿範鍛錛熶粖澶╁氨緇欏ぇ瀹跺垎浜涓涓嬫垜鍦ㄥ紑鍙戣繃紼嬩腑錛屾瘮杈冨父鐢ㄣ佷篃姣旇緝瀹炵敤鐨凩inux鍛戒護銆

01鏌ョ湅鏂囦歡鎴栨棩蹇

鏌ョ湅鏂囦歡鍐呭規垨鏃ュ織鏂囦歡錛岀浉淇″嚑涔庢墍鏈変漢閮戒細鎺ヨЕ鍒般傚叾涓錛岀敤寰楁渶澶氱殑灝辨槸head銆乼ail鍜宑at鍛戒護銆

1銆乭ead

琛ㄧず澶撮儴錛屾煡鐪嬫渶鍓嶉潰鍑犺屾暟鎹銆

headtest.log錛歵est.log鏂囦歡鍓嶅嚑琛屾棩蹇楀唴瀹

head-n20test.log錛氭煡鐪媡est.log鏂囦歡鍓20琛屾棩蹇楀唴瀹

head-n-20test.log錛氭煡鐪媡est.log闄や簡鏈鍚20琛屾棩蹇楀唴瀹

2銆乼ail

琛ㄧず灝鵑儴錛屾煡鐪嬫渶鍚庡嚑琛屾垨瀹炴椂鏃ュ織銆

tail-ftest.log錛氬疄鏃舵煡鐪媡est.log鏂囦歡鏈鍚庡嚑琛屾棩蹇楀唴瀹

tail-f-n20test.log錛氬疄鏃舵煡鐪媡est.log鏂囦歡鏈鍚20琛屾棩蹇楀唴瀹

tail-n20test.log錛氭煡鐪媡est.log鏂囦歡鏈鍚20琛屾棩蹇楀唴瀹

tail-n+20test.log錛氭煡鐪媡est.log鏂囦歡20琛屽悗鐨勬墍鏈夋棩蹇楀唴瀹

3銆乧at

鐢ㄤ簬鏌ョ湅鏂囦歡鍐呭廣

cattest.log錛氭煡鐪媡est.log鏂囦歡鎵鏈夊唴瀹

02鏈鍦板嶅埗

鍚屼竴鍙版湇鍔″櫒涓嶅悓鐩褰曚箣闂村嶅埗鏂囦歡鎴栫洰褰曚篃鏄緇忓父鐢ㄥ埌鐨勩

1銆佸嶅埗鏂囦歡

cptest.log/home/geshan/logs錛氬嶅埗test.log鏂囦歡鍒/home/geshan/logs/鐩褰曚笅

cptest.log/home/geshan/logs/cptest.log錛氬嶅埗test.log鏂囦歡鍒/home/geshan/logs/鐩褰曚笅騫墮噸鏂板懡鍚嶄負cptest.log

2銆佸嶅埗鏂囦歡澶

cp-rlogs//home/geshan/錛氬嶅埗logs鐩褰曞強鐩褰曚笅鎵鏈夋枃浠跺埌/home/geshan/鐩褰曚笅錛屾ゆ椂璇ョ洰褰曚細鏈塴ogs鐩褰

cp-rlogs//home/geshan/testlogs/錛氬嶅埗logs鐩褰曞強鐩褰曚笅鎵鏈夋枃浠跺埌/home/geshan/testlogs/鐩褰曚笅錛屽苟鎶妉ogs鐩褰曢噸鏂板懡鍚嶄負testlogs錛屽墠鎻愭槸/home/geshan/鐩褰曟病鏈塼estlogs鐩褰

娉錛-r鎴-R琛ㄧず寰鐜錛屽驚鐜鐩褰曚笅鎵鏈夋枃浠跺拰鐩褰曘備笅闈㈡墍鏈夊懡浠や篃鏄濡傛ゃ

03榪滅▼澶嶅埗

褰撴垜浠闇瑕佷粠榪滅▼鏈嶅姟鍣ㄥ嶅埗鏌愪釜鏂囦歡鎴栫洰褰曞埌鏈鍦扮櫥褰曠殑鏈嶅姟鍣ㄦ椂錛屽ぇ閮ㄥ垎浜烘兂鍒板厛鐧誨綍榪滅▼鏈嶅姟鍣ㄤ笅杞借ユ枃浠訛紝鐒跺悗鍐嶇櫥褰曟湰鍦版湇鍔″櫒鍐嶄笂浼犳枃浠躲傝繖鏍鋒槸姣旇緝楹葷儲錛屾晥鐜囦篃涓嶉珮銆

1銆佹湰鍦版湇鍔″櫒澶嶅埗鍒拌繙紼嬫湇鍔″櫒

(1)澶嶅埗鏂囦歡

鍛戒護1錛

scplocal_fileremote_username@remote_ip:remote_folder

鍛戒護2錛

scplocal_fileremote_username@remote_ip:remote_file

鍛戒護3錛

scplocal_fileremote_ip:remote_folder

鍛戒護4錛

scplocal_fileremote_ip:remote_file

絎1,2涓鎸囧畾浜嗙敤鎴峰悕錛屽懡浠ゆ墽琛屽悗闇瑕佽緭鍏ョ敤鎴峰瘑鐮侊紝絎1涓浠呮寚瀹氫簡榪滅▼鐨勭洰褰曪紝鏂囦歡鍚嶅瓧涓嶅彉錛涚2涓鎸囧畾浜嗘枃浠跺悕錛涚3,4涓娌℃湁鎸囧畾鐢ㄦ埛鍚嶏紝鍛戒護鎵ц屽悗榛樿や笌褰撳墠鏈嶅姟鍣ㄧ敤鎴峰悕騫墮渶瑕佽緭鍏ョ敤鎴峰瘑鐮侊紝絎3涓浠呮寚瀹氫簡榪滅▼鐨勭洰褰曪紝鏂囦歡鍚嶅瓧涓嶅彉錛岀4涓鎸囧畾浜嗘枃浠跺悕銆

瀹炰緥1錛氫粠鏈鍦版湇鍔″櫒澶嶅埗test.log鑷192.168.130.130鏈嶅姟鍣ㄧ殑/home/geshan/鐩褰曪紝騫墮噸鏂板懡鍚嶄負

20190125.log

鍛戒護錛

瀹炰緥2錛氫粠鏈鍦版湇鍔″櫒澶嶅埗test.log鑷192.168.130.130鏈嶅姟鍣ㄧ殑/home/geshan/鐩褰

鍛戒護錛

(2)澶嶅埗鐩褰

鍛戒護1錛

scp-rlocal_folderremote_username@remote_ip:remote_folder

鍛戒護2錛

scp-rlocal_folderremote_ip:remote_folder

絎1涓鎸囧畾浜嗙敤鎴峰悕錛屽懡浠ゆ墽琛屽悗闇瑕佽緭鍏ョ敤鎴峰瘑鐮侊紱絎2涓娌℃湁鎸囧畾鐢ㄦ埛鍚嶏紝鍛戒護鎵ц屽悗闇瑕佽緭鍏ョ敤鎴峰悕鍜屽瘑鐮併

瀹炰緥錛氫粠鏈鍦版湇鍔″櫒/home/appuser/鐩褰曚笅鐨則omcat鐩褰曞嶅埗鍒192.168.130.130鏈嶅姟鍣ㄧ殑/home/geshan/鐩褰曞懡浠わ細

2銆佷粠榪滅▼鏈嶅姟鍣ㄥ嶅埗鍒版湰鍦版湇鍔″櫒

浠庤繙紼嬪嶅埗鍒版湰鍦扮殑scp鍛戒護涓庝笂闈㈢殑鍛戒護闆峰悓錛屽彧瑕佸皢浠庢湰鍦板嶅埗鍒拌繙紼嬬殑鍛戒護鍚庨潰2涓鍙傛暟浜掓崲欏哄簭灝辮屼簡銆

(1)澶嶅埗鏂囦歡

瀹炰緥錛

浠192.168.130.130榪滅▼鏈嶅姟鍣ㄧ殑/home/geshan/璺寰勪笅test.log鏂囦歡澶嶅埗鍒版湰鍦版湇鍔″櫒/home/appuser/璺寰

鍛戒護錛

[email protected]:/home/geshan/test.log/home/appuser/

(2)澶嶅埗鐩褰

瀹炰緥錛氫粠192.168.130.130榪滅▼鏈嶅姟鍣ㄧ殑/home/geshan/璺寰勪笅tomcat鏂囦歡澶瑰嶅埗鍒版湰鍦版湇鍔″櫒/home/appuser/璺寰

鍛戒護錛

娉錛歩p鍦板潃鍜岃礬寰勪箣闂達紙鍗沖啋鍙蜂箣闂達級娌℃湁浠諱綍絀烘牸錛屽惁鍒欎細鎶ラ敊銆傚綋絎涓嬈″拰鍙﹀栦竴鍙版湇鍔¢氫俊錛堝傚嶅埗鏂囦歡錛夛紝浼氭彁紺烘槸鍚︿俊浠伙紝杈撳叆yes鍗沖彲銆

04鍘嬬緝鍜岃В鍘

鍘嬬緝鍜岃В鍘嬫枃浠朵篃鏄緇忓父閬囧埌鐨勩傚傛灉涓嬭澆涓涓鐩褰曚笅鐨勬墍鏈夋枃浠訛紝鍙浠ョ洿鎺ヤ笅杞借繖涓鐩褰曪紝浣嗘槸鐩存帴涓嬭澆鐩褰曠殑璇濓紝鏂囦歡涓鏃﹀緢澶氾紝涓嬭澆榪囩▼涓寰堝規槗涓㈠け鏂囦歡錛屼笅杞介熷害涔熸瘮杈冩參銆備竴鑸鎯呭喌涓嬫槸鍏堝帇緙╂枃浠跺す鍐嶄笅杞姐備笅闈㈢粰澶у朵粙緇嶅洓縐嶅帇緙╁拰瑙e帇鍛戒護銆

1銆乼ar鍛戒護

瑙e帇錛

鍘嬬緝錛

瀹炰緥錛氳В鍘嬩竴涓猯og.tar鏂囦歡錛

鍘嬬緝log鐩褰曞苟鍛藉悕涓簂og.tar錛

2銆乬z鍛戒護

鍘嬬緝錛

娉錛氬帇緙╀負.gz鏂囦歡婧愭枃浠朵細娑堝け濡傛灉鎯充繚鐣欐簮鏂囦歡錛屼嬌鐢ㄤ笅闈㈠懡浠

瑙e帇錛

.tar.gz鍜.tgz瑙e帇錛

鍘嬬緝錛

鍘嬬緝澶氫釜鏂囦歡錛

瀹炰緥錛氳В鍘媡omcat.tar.gz鏂囦歡

鍘嬬緝tomcat鐩褰曪紝騫跺懡鍚嶄負tomcat.tar.gz鏂囦歡

3銆亃ip鍛戒護

瑙e帇錛歶nzipfileName.zip鍘嬬緝錛

娉錛氬帇緙╃洰褰曟椂錛岄渶鍔-r錛岃〃紺哄驚鐜璇ョ洰褰曚笅鎵鏈夋枃浠跺拰鐩褰曘傚帇緙╁氫釜鏂囦歡鎴栫洰褰曟椂錛屾枃浠舵垨鐩褰曚箣闂撮渶瑕佺┖鏍箋

瀹炰緥錛氳В鍘嬩竴涓猯og.zip鏂囦歡錛

鍘嬬緝log鏂囦歡鐩褰曞苟鍛藉悕涓簂og.zip錛

鍘嬬緝log1鍜宭og2鐩褰曞苟鍛藉悕涓簂og.zip錛

鍘嬬緝test1.log鍜宼est2.log鏂囦歡騫跺懡鍚嶄負log.zip錛

4銆乺ar鍛戒護

鍚岀悊錛屽拰zip鍛戒護綾諱技錛屽彧闇鎶妟ip鏀逛負rar鍗沖彲銆

05淇鏀規潈闄

Linux緋葷粺涓鏂囦歡鍜岀洰褰曡塊棶鏉冮檺鏄涓嶅悓鐨勶紝閫氳繃浜涙柟寮忓彲浠ョ『瀹氳皝鍙浠ュ硅ユ枃浠跺拰鐩褰曡繘琛岀浉搴旂殑鎿嶄綔銆

鍏朵腑錛屾枃浠舵垨鐩褰曠殑璁塊棶鏉冮檺鍒嗕負鍙璇伙紝鍙鍐欏拰鍙鎵ц屼笁縐嶏紝鍒嗗埆浠h〃鍙璇匯佸彲鍐欏拰鍙鎵ц屻

1銆乧hgrp鍛戒護

鏀瑰彉鏂囦歡鎴栫洰褰曟墍灞炵殑緇勩傚懡浠わ細

瀹炰緥錛氭妸test.log鏂囦歡鎵灞炵殑緇勪慨鏀逛負geshan緇

鎶妉ogs鐩褰曞強璇ョ洰褰曚笅鎵鏈夋枃浠舵墍灞炵殑緇勪慨鏀逛負geshan緇

2銆乧hown鍛戒護

鏇存敼鏌愪釜鏂囦歡鎴栫洰褰曟墍灞炵殑鐢ㄦ埛銆傚懡浠わ細

瀹炰緥錛氭妸test.log鏂囦歡鎵灞炵敤鎴蜂慨鏀逛負geshan鐢ㄦ埛

鎶妉ogs鐩褰曞強璇ョ洰褰曚笅鎵鏈夋枃浠舵墍灞炵敤鎴蜂慨鏀逛負geshan鐢ㄦ埛

3銆乧hmod鍛戒護

鐢ㄤ簬鏀瑰彉鏂囦歡鎴栫洰褰曠殑璁塊棶鏉冮檺銆傜敤鎴風敤瀹冩帶鍒舵枃浠舵垨鐩褰曠殑璁塊棶鏉冮檺銆傚懡浠わ細

鍏朵腑錛宮ode鏈夊緢澶氱嶈〃紺哄惈涔夛紝涓嬮潰鍙瀵規暟瀛楄存槑銆傚ぇ閮ㄥ垎浜虹湅鍒癿ode鏈777鎴776絳夋潈闄愶紝榪欎簺琛ㄧず浠涔堝憿錛

涓轟簡鏂逛究錛屾妸mode鏀逛負abc錛屽叾涓璦,b,c鍚勪負涓涓鏁板瓧錛屽垎鍒琛ㄧずuser銆乬roup鍙妎ther鐨勬潈闄愩俽=4錛寃=2錛寈=1錛宺琛ㄧずRead璇伙紝w琛ㄧずWrite鍐欙紝x琛ㄧずeXecute銆

鑻ヨ乺wx灞炴у垯4+2+1=7錛

鑻ヨ乺w-灞炴у垯4+2=6錛

鑻ヨ乺-x灞炴у垯4+1=5銆

瀹炰緥錛氭妸test.log鏂囦歡淇鏀瑰彲璇誨啓鎵ц岋紙鍗777錛夋搷浣溿

鎶妉ogs鐩褰曞強鐩褰曚笅鎵鏈夋枃浠朵慨鏀逛負璇誨啓錛666錛夋搷浣溿

鍦↙inux緋葷粺涓杈撳叆ll(涓や釜灝忓啓LL)錛屽傛灉鏂囦歡鎴栫洰褰曚負777錛堣誨啓鎵ц岋級鏉冮檺錛屽彲鐪嬪埌鏈鍓嶉潰涓-rwxrwxrwx銆傚傛灉涓666錛堣誨啓錛夋潈闄愶紝鍙鐪嬪埌鏈鍓嶉潰涓-rw-rw-rw銆

娉錛氫竴鑸淇鏀規枃浠舵垨鐩褰曟潈闄愰渶瑕佽秴綰х敤鎴鳳紙root錛夎繘琛屼慨鏀癸紝鍚﹀垯鍙鑳戒細鎶ユ潈闄愪笉瓚熾

榪欎簺Linux鍛戒護鏄姣旇緝甯哥敤瀹炵敤鐨勫懡浠わ紝鑲瀹氫細鏈夌敤寰楀埌鐨勬椂鍊欙紝濡傛灉瀵逛綘鏈夋墍甯鍔╋紝璁板緱鐐硅禐鏀惰棌杞鍙戝摝錛

Ⅱ 編寫shell腳本,輸入n,求1-n的累加和

#!/bin/bash
read-p"Inputvalueofn:"n
sum=0
i=1
while(($i<=$n))
do
sum=$(($sum+$i))
i=$(($i+1))
done
echo"SUMis$sum."

Ⅲ shell 腳本裡面從一個文本裡面讀出一個數字,如何轉換成整數我需要用這個數字進行加減乘除

可以參考下面幾種方法:

法一:

echo ${var%.*} #這個是直接去除小數點及後面所有內容,只用於bash

法二:

echo $var | awk -F. '{print $1}' #以小數點為分隔符取第一個欄位

法三:

echo $var | awk '{print int($0)}' #awk中可直接使用C函數取整

(3)shell腳本加減擴展閱讀:

shell腳本

shell script是利用shell的功能所寫的一個程序,這個程序是使用純文本文件,將一些shell的語法與指令寫在裡面,然後用正規表示法,管道命令以及數據流重導向等功能,以達到我們所想要的處理目的。

更明白地來說,shell script就像早期dos年代的.bat,最簡單的功能就是將許多指令匯整寫一起,讓使用者很容易地就能夠一個操作執行多個命令。

Ⅳ shell腳本里的變數怎麼加1

shell腳本中的整數型變數加1的方式如下:

#!/bin/sh
#本腳本測試shell腳本中整型變數自增加1的幾種方法

#定義整型變數
a=1
echo$a

#第一種整型變數自增方式
a=$(($a+1))
echo$a

#第二種整型變數自增方式
a=$[$a+1]
echo$a

#第三種整型變數自增方式
a=`expr$a+1`
echo$a

#第四種整型變數自增方式
leta++
echo$a

#第五種整型變數自增方式
leta+=1
echo$a

#第六種整型變數自增方式
((a++))
echo$a

Shell腳本與Windows/Dos下的批處理相似,也就是用各類命令預先放入到一個文件中,方便一次性執行的一個程序文件,主要是方便管理員進行設置或者管理用的。但是它比Windows下的批處理更強大,比用其他編程程序編輯的程序效率更高,它使用了Linux/Unix下的命令。

Ⅳ 在Linux下,用shell編寫一個簡單的計算器,要實現加減乘除4個功能就行了

不用寫吧,本來有個 bc 命令可用,沒有下載就成.
非要寫一個,zsh 的function里有一個,名 zcalc,
貼上來給你

#!/usr/bin/zsh -i
#
# Zsh calculator. Understands most ordinary arithmetic expressions.
# Line editing and history are available. A blank line or `q' quits.
#
# Runs as a script or a function. If used as a function, the history
# is remembered for reuse in a later call (and also currently in the
# shell's own history). There are various problems using this as a
# script, so a function is recommended.
#
# The prompt shows a number for the current line. The corresponding
# result can be referred to with $<line-no>, e.g.
# 1> 32 + 10
# 42
# 2> $1 ** 2
# 1764
# The set of remembered numbers is primed with anything given on the
# command line. For example,
# zcalc '2 * 16'
# 1> 32 # printed by function
# 2> $1 + 2 # typed by user
# 34
# 3>
# Here, 32 is stored as $1. This works in the obvious way for any
# number of arguments.
#
# If the mathfunc library is available, probably understands most system
# mathematical functions. The left parenthesis must be adjacent to the
# end of the function name, to distinguish from shell parameters
# (translation: to prevent the maintainers from having to write proper
# lookahead parsing). For example,
# 1> sqrt(2)
# 1.4142135623730951
# is right, but `sqrt (2)' will give you an error.
#
# You can do things with parameters like
# 1> pi = 4.0 * atan(1)
# too. These go into global parameters, so be careful. You can declare
# local variables, however:
# 1> local pi
# but note this can't appear on the same line as a calculation. Don't
# use the variables listed in the `local' and `integer' lines below
# (translation: I can't be bothered to provide a sandbox).
#
# Some constants are already available: (case sensitive as always):
# PI pi, i.e. 3.1415926545897931
# E e, i.e. 2.7182818284590455
#
# You can also change the output base.
# 1> [#16]
# 1>
# Changes the default output to hexadecimal with numbers preceded by `16#'.
# Note the line isn't remembered.
# 2> [##16]
# 2>
# Change the default output base to hexadecimal with no prefix.
# 3> [#]
# Reset the default output base.
#
# This is based on the builtin feature that you can change the output base
# of a given expression. For example,
# 1> [##16] 32 + 20 / 2
# 2A
# 2>
# prints the result of the calculation in hexadecimal.
#
# You can't change the default input base, but the shell allows any small
# integer as a base:
# 1> 2#1111
# 15
# 2> [##13] 13#6 * 13#9
# 42
# and the standard C-like notation with a leading 0x for hexadecimal is
# also understood. However, leading 0 for octal is not understood --- it's
# too confusing in a calculator. Use 8#777 etc.
#
# Options: -#<base> is the same as a line containing just `[#<base>],
# similarly -##<base>; they set the default output base, with and without
# a base discriminator in front, respectively.
#
#
# To do:
# - separate zcalc history from shell history using arrays --- or allow
# zsh to switch internally to and from array-based history.

emulate -L zsh
setopt extendedglob

local line ans base defbase forms match mbegin mend psvar optlist opt arg
local compcontext="-math-"
integer num outdigits outform=1
# We use our own history file with an automatic pop on exit.
history -ap "${ZDOTDIR:-$HOME}/.zcalc_history"

forms=( '%2$g' '%.*g' '%.*f' '%.*E' )

zmodload -i zsh/mathfunc 2>/dev/null

: ${ZCALCPROMPT="%1v> "}

# Supply some constants.
float PI E
(( PI = 4 * atan(1), E = exp(1) ))

# Process command line
while [[ -n $1 && $1 = -(|[#-]*) ]]; do
optlist=${1[2,-1]}
shift
[[ $optlist = (|-) ]] && break
while [[ -n $optlist ]]; do
opt=${optlist[1]}
optlist=${optlist[2,-1]}
case $opt in
('#') # Default base
if [[ -n $optlist ]]; then
arg=$optlist
optlist=
elif [[ -n $1 ]]; then
arg=$1
shift
else
print "-# requires an argument" >&2
return 1
fi
if [[ $arg != (|\#)[[:digit:]]## ]]; then
print - "-# requires a decimal number as an argument" >&2
return 1
fi
defbase="[#${arg}]"
;;
esac
done
done

for (( num = 1; num <= $#; num++ )); do
# Make sure all arguments have been evaluated.
# The `$' before the second argv forces string rather than numeric
# substitution.
(( argv[$num] = $argv[$num] ))
print "$num> $argv[$num]"
done

psvar[1]=$num
while vared -cehp "${(%)ZCALCPROMPT}" line; do
[[ -z $line ]] && break
# special cases
# Set default base if `[#16]' or `[##16]' etc. on its own.
# Unset it if `[#]' or `[##]'.
if [[ $line = (#b)[[:blank:]]#('[#'(\#|)(<->|)']')[[:blank:]]#(*) ]]; then
if [[ -z $match[4] ]]; then
if [[ -z $match[3] ]]; then
defbase=
else
defbase=$match[1]
fi
print -s -- $line
line=
continue
else
base=$match[1]
fi
else
base=$defbase
fi

print -s -- $line

case ${${line##[[:blank:]]#}%%[[:blank:]]#} in
q) # Exit if `q' on its own.
return 0
;;
norm) # restore output format to default
outform=1
;;
sci[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=2
;;
fix[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=3
;;
eng[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=4
;;
local([[:blank:]]##*|))
eval $line
line=
continue
;;
*)
# Latest value is stored as a string, because it might be floating
# point or integer --- we don't know till after the evaluation, and
# arrays always store scalars anyway.
#
# Since it's a string, we'd better make sure we know which
# base it's in, so don't change that until we actually print it.
eval "ans=\$(( $line ))"
# on error $ans is not set; let user re-edit line
[[ -n $ans ]] || continue
argv[num++]=$ans
psvar[1]=$num
;;
esac
if [[ -n $base ]]; then
print -- $(( $base $ans ))
elif [[ $ans = *.* ]] || (( outdigits )); then
printf "$forms[outform]\n" $outdigits $ans
else
printf "%d\n" $ans
fi
line=
done

return 0

支援小數點,+ - * / , ok

Ⅵ 剛學shell腳本寫了個小 計算器,加減除都可以,乘法不行,哪兒錯了

你的問題在:elif [ $fangfa = " \* " ]; 這句並不能判斷到「*「乘號;
正確的結果如下:
#!/bin/bash
echo "first number"
read a
echo "fangfa"
read fangfa
echo "second number"
read b
if [ "$fangfa" = "+" ];then
echo $(($a+$b))
elif [ "$fangfa" = "-" ];then
echo $(($a-$b))
elif [ "$fangfa" = "*" ];then
echo $(($a*$b))
elif [ "$fangfa" = "/" ];then
echo $(($a/$b))
fi
===============================================
腳本優化版本:
#!/bin/bash

read -p "input first number: " num1
read -p "input operator: " operator
read -p "input second number: " num2

if [ "$operator" == "+" ];then
echo " num1 + num2 = $(($num1+$num2))"
elif [ "$operator" == "-" ];then
echo "num1 - num2 = $(($num1-$num2))"
elif [ "$operator" == "*" ];then
echo "num1 * num2 = $(($num1*$num2))"
elif [ "$operator" == "/" ];then
echo "num1 / num2 = $(($num1/$num2))"
fi

熱點內容
榮耀v20升級存儲 發布:2024-07-27 09:20:19 瀏覽:485
安卓用什麼和電腦傳圖片 發布:2024-07-27 09:02:07 瀏覽:288
存儲過程就是 發布:2024-07-27 08:56:51 瀏覽:131
c語言高級試題 發布:2024-07-27 08:48:30 瀏覽:282
ip伺服器世界上有幾台 發布:2024-07-27 08:46:18 瀏覽:394
金立手機怎麼清理緩存 發布:2024-07-27 08:38:50 瀏覽:311
iphone文件夾不顯示 發布:2024-07-27 08:18:05 瀏覽:774
y510p固態硬碟做緩存 發布:2024-07-27 07:59:34 瀏覽:128
奶塊為什麼進伺服器會排隊 發布:2024-07-27 07:57:15 瀏覽:691
資料庫表標識 發布:2024-07-27 07:50:00 瀏覽:923