当前位置:首页 » 编程软件 » 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

热点内容
频率计源码 发布:2024-09-08 07:40:26 浏览:778
奥迪a6哪个配置带后排加热 发布:2024-09-08 07:06:32 浏览:100
linux修改apache端口 发布:2024-09-08 07:05:49 浏览:208
有多少个不同的密码子 发布:2024-09-08 07:00:46 浏览:566
linux搭建mysql服务器配置 发布:2024-09-08 06:50:02 浏览:995
加上www不能访问 发布:2024-09-08 06:39:52 浏览:811
银行支付密码器怎么用 发布:2024-09-08 06:39:52 浏览:513
苹果手机清理浏览器缓存怎么清理缓存 发布:2024-09-08 06:31:32 浏览:554
云服务器的优点与缺点 发布:2024-09-08 06:30:34 浏览:734
上传下载赚钱 发布:2024-09-08 06:14:51 浏览:258