導航:首頁 > 操作系統 > linuxshell加減

linuxshell加減

發布時間:2023-05-24 05:20:09

⑴ shell 編程負數加減運算

法一:用巧胡let,它幾乎支持所有的運算符
let c=a+b
echo $c
結果就是1

法二拍汪:如果是Bash,可以孝賀攔用運算符$(( ))
c=$((a+b))
echo $c
結果也是1

linux最常用的Shell命令

有些人仍然會有這種愚蠢的想法,他們認為使用Linux就必須使用Linux
shell命令。胡說!你可以不懂得任何Linux命令,比如說ps,grep,ls等,但是你仍然可以使用很多現代的Linux桌面發行版。
Linux的系統管理員與桌面用戶不一樣,他們不像桌面用戶一樣使用Cinnamon,GNOME,Unity,或者KDE,他們所有的時間都是用Linux命令。
對於桌面用戶來講,若是了解一部分Linux命令,你可以更好的使用Linux,體驗它的魅力,下面列舉出了一些:
Shell基礎:
你可以通過打開Linux的terminal(終端)來執行Shell命令。Shell的種類有很多種,例如CSH,Bourne
Shell,Korn
Shell。在現在的大多數Linux發行版中,默認的Shell一般都是Bourne
again
shell(bash)。
想看看你的Shell是哪一種,執行下面的命令
echo
$SHELL
在Linux中,$符號代表一個shell變數。所有的shell都用這種方式使用變數。有一些shell變數在你的系統啟動的時候就有了默認值。例如,$SHELL;$LOGNAME是你的登錄名,而$PATH變數指明了你的shell命令的搜索范圍。
echo命令的作用就是列印出你的輸入。如果你的輸入具有shell的特殊意義,例如shell變數,他就輸出變數的值。

一個重要的地方是,你要注意文本的大小寫。例如,ls,是DOS的dir命令的Linux版本。這個命令列出當前工作目錄下的文件列表。如果你輸入的是LS,你得到的只能是「找不到命令」的錯誤信息。
另外在Linux
shell命令中一個重要的地方是,你可以將命令串起來。這是Unix/Linux從第一天開始就有的巧妙的特點。最簡單的將命令連起來的辦法就是使用「|」,我們稱之為「pipe」。第一個命令的輸出就是下一個命令的輸入。
Linux命令有自己的語法規則:
基本的語法就像這樣:
command
-option
file
例如:
ls
-la
這行命令的意義是輸出當前目錄的所有文件的文件名,l代表「long」,a代表「all」,有了l選項,你會發現,輸出的內容比較豐富,不只包括文件
名,還有文件的訪問許可權,所有者,所屬組等。你會發現這個命令會在屏幕上輸出大量的信息,如果當前目錄的文件比較多的話。
現在就是「pipe」出場的時候了。
ls
-la
|
more
你會在屏幕上看到如下信息:

你也可以在大多數Linux命令中使用通配符。通配符就是可以代表文件名中任何未知的字元或字元串。例如,*就代表任意字元串,?代表單個字元。例如:

⑶ 在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

⑷ linux shell兩個文件數值相加

#!/bin/bash
n=1
foriin`cat1.txt`;do#手大循環1.txt
num=`sed-n"遲旅${n}p"2.txt`#獲取2.txt對行的數字
sum=$(($num+$i))#兩數畢旦豎相加
echo$sum#列印結果
n=$(($n+1))
done

⑸ 1.linux系統下shell腳本用case語句編寫四則運算 2.linux系統下shell腳本輸入數字串。進行反序輸出

原來我.shell寫的侍耐桐畝孝計算器:
[root@liuxiting testdir]# cat calculator.sh
#!/bin/bash
echo "usage: 1+3 <Enter> ,q <Enter> is quit"
while [ 1 ]
do
read -p "->>" str 1>>/dev/null
a=`echo $str |awk -F '+|-|*|/' '{print $1}'`
if [ $a == q ]
then
break
fi
b=`echo $str |awk -F '+|-|*|/' '{print $2}'`
o=`echo $str |grep -o "[[:punct:]]" | grep -v "\."`
case $o in
+) awk 'BEGIN{printf " =%.2f\n",'$a'+'$b'}';;
-) awk 'BEGIN{printf " =%.2f\n",'$a'-'$b'}';;
\*) awk 'BEGIN{printf " =%.2f\n",'$a'*'$b'}';;
/)if [ $b -eq 0 ]
then
echo 0 Can NOT be denominator!
continue
fi
awk 'BEGIN{printf " =%.2f\n",'$a'/'$b'}';;
#^) awk 'BEGIN{printf " =%.2f\n",'$a'**'$b'}';;
*) echo error;;
esac
done

剛剛寫的老坦倒序輸出:
[root@liuxiting testdir]# cat XuShuChu.sh
#!/bin/bash
echo "usage: 123456 <Enter>, q <Enter> is quit"
while [ 1 ]
do
echo -n "Pleasw enter number : "
read n
if [ $n == 'q' ]
then
break
fi
sd=0
rev=""
on=$n
echo "$n"
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
rev=$( echo $rev$sd)
done
echo "$on in a reverse order $rev"
done

⑹ linux用shell編1+2+3+...+n

題主你好,下面圖片是代碼及相應截圖:

寫在最後:將上面的代碼寫到一個文件中,比如文件名碼悄為:

sum.sh

給該文件加上可執行遲弊渣許可權:

chmod +x sum.sh

最後執行該文件:

./sum.sh n //n表示你要求多少個數之和

希望可以幫卜簡到題主, 歡迎追問

⑺ linux shell 兩個文件內容做加減法

awk'{getlines<"第一個文件"
split(s,array)
for(i=1;i<=NF;i++)$i=$i-array[5+i]
print}'第運握陸二個旁頃文件

結果皮液

9761 98.895 10 1 98.896

閱讀全文

與linuxshell加減相關的資料

熱點內容
單片機編程取反 瀏覽:895
51單片機課程設計課題 瀏覽:897
手機淘寶登錄怎麼加密碼 瀏覽:484
linux快捷方式圖標 瀏覽:37
陽光車險的app叫什麼名字 瀏覽:461
購買單片機的器件時需要給商家啥 瀏覽:534
並行編譯技術的發展 瀏覽:549
阿里雲伺服器安裝管理 瀏覽:550
java手機開發教程 瀏覽:674
我的世界怎麼刪除伺服器數據 瀏覽:671
linux內存子系統 瀏覽:972
加密思維幣 瀏覽:690
魅族訪客文件夾 瀏覽:52
添加的文件夾怎麼找 瀏覽:617
程序員涉黃 瀏覽:700
maven編譯resources下的js 瀏覽:521
ubuntu文件移動命令 瀏覽:229
安卓i怎麼查找蘋果手機 瀏覽:951
雲伺服器宕機概率 瀏覽:232
在線買葯用什麼app知乎 瀏覽:815