The error message is caused by the line a=$(($ex / $f)), because there is no operand following the / (divide) operator. So the immediate suspicion is that the variable f is an empty string.
The cause of that is the behaviour of the $( ) command substitution when f is assigned. This collects the contents of stdout when the enclosed command pipeline is executed. As your factorial() function writes nothing to stdout, the value assigned to f is empty. The solution is to echo or print the value, in place of the return — e.g. as printf '%sn' "${fact}".
Some additional notes that might be helpful:
The value in the return statement represents the status of the function, not any data it may have produced. In the absence of a return statement, the exit status of the last command executed in the function is passed back to the caller.
The return status is truncated to 8 bits, and is unsigned, so it can only take the range 0-255.
In addition, the status of external commands has further conventions. Processes terminated by a signal get a status of 128 + the signal number. If the shell fails to create a new process, or to execute the command, 126 or 127 may be returned. Status 0 is conventionally success, and any small integer from 1 up indicates a command-detected error or unusual result. I would consider it wise to follow the same conventions in my shell functions, and never to return a data value this way.
Shell arithmetic is (according to the GNU/bash manual) evaluated in fixed-width integers. On my system, that appears to be 64-bit signed integers, but it may vary across systems and distros. 31 bits is only sufficient to hold 12!, and 63 bits only holds 20!, which may limit your scope.
This fragment shows Bash arithmetic overflowing 63 bits, somewhere around 9.22e+18.
Paul--) for k in {1..10}; do
> printf '%s %sn' $k $(( 3000000000000000000 * k ))
> done
1 3000000000000000000
2 6000000000000000000
3 9000000000000000000
4 -6446744073709551616
5 -3446744073709551616
6 -446744073709551616
7 2553255926290448384
8 5553255926290448384
9 8553255926290448384
10 -6893488147419103232
Paul--)
My go-to for large numbers is the dc command, which does unconstrained numeric size, but requires RPN (reverse polish notation). This fragment generates the RPN commands to list factorials up to 10!
Paul--) { echo 1; seq -s ' p * p ' 2 10; echo ' p * p q'; }
1
2 p * p 3 p * p 4 p * p 5 p * p 6 p * p 7 p * p 8 p * p 9 p * p 10
p * p q
Paul--)
and this is the execution (but I tested up to 400!):
Paul--) { echo 1; seq -s ' p * p ' 2 10; echo ' p * p q'; } | dc
2
2
3
6
4
24
5
120
6
720
7
5040
8
40320
9
362880
10
3628800
Paul--)
I’m writing a script in bash and I get this error:
./P4.1: line 10: +: syntax error: operand expected (error token is "+")
And this is my code:
#!/bin/bash
read string
echo $string >| temp
num1= cut -d" " -f1 temp
num2= cut -d" " -f2 temp
num3= cut -d" " -f3 temp
while [ $num1 -gt $num3 ]
do
echo $num1
num1=$[$num1+$num2]
done
What’s wrong and how do I fix it?
Thanks.
asked Nov 24, 2013 at 16:03
1
Combination of ceving and Tomek’s:
#!/bin/bash
read num1 num2 num3
while [ $num1 -lt $num3 ]
do
echo $num1
num1=$((num1+num2))
done
answered Nov 24, 2013 at 16:32
![]()
Blue IceBlue Ice
7,8186 gold badges31 silver badges51 bronze badges
4
Use round parenthesis for numeric computations:
num1=$((num1 + num2))
BoltClock
686k158 gold badges1372 silver badges1347 bronze badges
answered Nov 24, 2013 at 16:10
![]()
cevingceving
21.1k11 gold badges97 silver badges166 bronze badges
0
#!/bin/bash
read string
echo "${string}" >| temp
num1= cut -d" " -f1 temp
num2= cut -d" " -f2 temp
num3= cut -d" " -f3 temp
while [ "${num1}" -gt "${num3}" ]
do
echo "${num1}"
num1=$(expr "${num1}" + 1)
done
also, quote and brace your variables. 😀
answered Nov 24, 2013 at 16:09
petrus4petrus4
6164 silver badges7 bronze badges
0
try
num1=$[ num1 + num2 ]
may remove the error ~
answered Jun 30, 2022 at 1:49
I am getting this error bellow:
Path to the shell file:line 6: ++++: syntax error: operand expected (error token is "+")
and
Path to the shell file:line 13: ((: i <= : syntax error: operand expected (error token is "<= ")
This is my script:
#!/bin/bash
SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )
file="$SCRIPTPATH/android/sdcard.img"
file2="$SCRIPTPATH/android/devices.txt"
TOTALDEVICES=$(($1+$2+$3+$4+$5))
ANDROID4=0
ANDROID5=0
ANDROID5_1=0
ANDROID6=0
ANDROID7=0
echo $TOTALDEVICES
for ((i = 1; i <= $TOTALDEVICES; i++));
do
if (($1 > 0 && $ANDROID4 < $1))
then
echo "Device$i PACKAGE(avd4.4) 1"
ANDROID4=$((ANDROID4 + 1))
echo "no" |~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-19;google_apis;armeabi-v7a' --name "avd4" --tag 'google_apis' -p $SCRIPTPATH/android/avd4
fi
if (($2 > 0 && $ANDROID5 < $2 && $ANDROID4 == $1 && $i > $ANDROID4))
then
echo "Device$i PACKAGE(avd5.0) 2"
ANDROID5=$((ANDROID5 + 1))
echo "no" |~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-21;google_apis;armeabi-v7a' --name "avd5" --tag 'google_apis' -p $SCRIPTPATH/android/avd5
fi
if (($3 > 0 && $ANDROID5_1 < $3 && $ANDROID5 == $2 && $i > $ANDROID5 + $ANDROID4))
then
echo "Device$i PACKAGE(avd5.1) 3"
ANDROID5_1=$((ANDROID5_1 + 1))
echo "no" |~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-22;google_apis;x86' --name "avd5.1" --tag 'google_apis' -p $SCRIPTPATH/android/avd5.1
fi
if (($4 > 0 && $ANDROID6 < $4 && $ANDROID5_1 == $3 && $i > $ANDROID5_1 + $ANDROID5 + $ANDROID4))
then
echo "Device$i PACKAGE(avd6) 4"
ANDROID6=$((ANDROID6 + 1))
echo "no" | ~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-23;google_apis;x86' --name "avd6" --tag 'google_apis' -p $SCRIPTPATH/android/avd6
fi
if (($5 > 0 && $ANDROID7 < $5 && $ANDROID6 == $4 && $i > $ANDROID6 + $ANDROID5_1 + $ANDROID5 + $ANDROID4))
then
echo "Device$i PACKAGE(avd7) 5"
ANDROID7=$((ANDROID7 + 1))
echo "no" | ~/Android/Sdk/tools/bin/avdmanager create avd -f --package 'system-images;android-24;google_apis;x86' --name "avd7" --tag 'google_apis' -p $SCRIPTPATH/android/avd7
fi
done
AVDMANAGEROUTPUT=$(~/Android/Sdk/tools/bin/avdmanager list avds | grep "Name:")
AVDMANAGEROUTPUT=${AVDMANAGEROUTPUT//$'n'/} # Remove all newlines.
AVDMANAGEROUTPUT=${AVDMANAGEROUTPUT%$'n'} # Remove a trailing newline.
DEVICES=()
i=0
IFS=' ' read -r -a array <<< "$AVDMANAGEROUTPUT"
for index in "${!array[@]}"
do
rem=$(( $index % 2 )) #check for odd number to avoid Name:
if [ $rem -eq 1 ]
then
echo "${array[index]}" #Now put values into an array
DEVICES[$i]=`echo @"${array[index]}"`
i=$((i+1))
fi
done
# Check if the sdcard is available
if [ -f "$file" ]
then
echo "$file found."
~/Android/Sdk/emulator/emulator ${DEVICES[0]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[1]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[2]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[3]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[4]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
sleep 110
else
# if there is none,here we are creating one
echo "$file not found."
~/Android/Sdk/emulator/mksdcard 10G "$SCRIPTPATH/android/sdcard.img"
sleep 5
~/Android/Sdk/emulator/emulator ${DEVICES[0]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 &
~/Android/Sdk/emulator/emulator ${DEVICES[1]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[2]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[3]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
~/Android/Sdk/emulator/emulator ${DEVICES[4]} -sdcard $file >> "$SCRIPTPATH/emulators.txt" 2>&1 & #open the emulator
sleep 110
fi
![]()
Zanna♦
68.1k55 gold badges209 silver badges319 bronze badges
asked May 4, 2017 at 6:41
1
Line 6 of your script expects arguments 1 to 5 to exist. If you run your script with no arguments $1+$2+$3+$4+$5 will become +++++ which explains the error message. (A similar explanation is valid for the second error message.) Perhaps you should check that 5 arguments have been provided and exit with an error message if this test fails, e. g.:
if [ $# != 5 ]
then
echo "Usage: scriptname num1 num2 num3 num4 num5"
exit 2
fi
Alternatively you can provide a default value for missing arguments with Bash’s parameter expansion. In the following expression the first 5 command-line arguments, or 0 in their absence, are added to a sum:
$((${1-0} + ${2-0} + ${3-0} + ${4-0} + ${5-0}))
Note that you should also check that each of the 5 arguments is numeric before attempting to obtain their sum.
![]()
answered May 4, 2017 at 9:00
![]()
Thank you for your answer, you helped me see what I did wrong.
How did I fix it:
I then ran the sh file like ‘file.sh’ 1 1 1 1 1 which had enabled the emulators to run. The 1s determine how many emulators of each do I want to run.
answered May 9, 2017 at 13:51
0
I had a script that was doing well until now, I don’t now what has changed, but the script is the same.
I had this variables:
SIZEKB=$[[(SIZE+1023)/1024]]
REQUIREDKB=$[[SIZEKB+MINFREEKB]]
Which I repeat were working ok, but now I got this error:
line 12: [(SIZE+1023)/1024]: syntax error: operand expected (error token is "[(SIZE+1023)/1024]")
line 13: [SIZEKB+MINFREEKB]: syntax error: operand expected (error token is "[SIZEKB+MINFREEKB]")
What should I change to correct the syntax, thanks!
asked Dec 17, 2020 at 10:43
Andrés ChandíaAndrés Chandía
9771 gold badge16 silver badges32 bronze badges
7
The easiest way to write those assignment is to providean arithmetic context:
((SIZEKB=(SIZE+1023)/1024))
((REQUIREDKB=SIZEKB+MINFREEKB))
You can use also
SIZEKB=$[(SIZE+1023)/1024]
REQUIREDKB=$[SIZEKB+MINFREEKB]
but this is deprecated and provided for compatibility with old scripts (perhaps those written for bash version 1?).
answered Dec 17, 2020 at 11:27
You need to place the equations in two curly brackets and so:
REQUIREDKB=$(( $SIZEKB+$MINFREEKB ))
Alternatively, you can use single square brackets and so:
REQUIREDKB=$[ $SIZEKB+$MINFREEKB ]
![]()
answered Dec 17, 2020 at 10:56
![]()
Raman SailopalRaman Sailopal
12.1k2 gold badges8 silver badges17 bronze badges
I had a script that was doing well until now, I don’t now what has changed, but the script is the same.
I had this variables:
SIZEKB=$[[(SIZE+1023)/1024]]
REQUIREDKB=$[[SIZEKB+MINFREEKB]]
Which I repeat were working ok, but now I got this error:
line 12: [(SIZE+1023)/1024]: syntax error: operand expected (error token is "[(SIZE+1023)/1024]")
line 13: [SIZEKB+MINFREEKB]: syntax error: operand expected (error token is "[SIZEKB+MINFREEKB]")
What should I change to correct the syntax, thanks!
asked Dec 17, 2020 at 10:43
Andrés ChandíaAndrés Chandía
9771 gold badge16 silver badges32 bronze badges
7
The easiest way to write those assignment is to providean arithmetic context:
((SIZEKB=(SIZE+1023)/1024))
((REQUIREDKB=SIZEKB+MINFREEKB))
You can use also
SIZEKB=$[(SIZE+1023)/1024]
REQUIREDKB=$[SIZEKB+MINFREEKB]
but this is deprecated and provided for compatibility with old scripts (perhaps those written for bash version 1?).
answered Dec 17, 2020 at 11:27
You need to place the equations in two curly brackets and so:
REQUIREDKB=$(( $SIZEKB+$MINFREEKB ))
Alternatively, you can use single square brackets and so:
REQUIREDKB=$[ $SIZEKB+$MINFREEKB ]
![]()
answered Dec 17, 2020 at 10:56
![]()
Raman SailopalRaman Sailopal
12.1k2 gold badges8 silver badges17 bronze badges
Could anyone point out what’s wrong with the below line from a bash script I’m using?
if [ "$(( $(date +"%s")-$(stat -c "%Y" $SENDDIR/$NIGHTLY_FILE) ))" -gt "3600" ]; then
I receive the error noted in the subject line.
I had copied the script from one server to another thinking that it should work but alas, no.
- bash
- operand
![]()
Josh Crozier
228k54 gold badges386 silver badges301 bronze badges
asked Dec 9, 2013 at 1:45
PeteJPeteJ
13 bronze badges
5
-
wrap that line with
set -vxabove andset +vxbelow so you can see how the variables are being expanded. Then it should be very easy to see where the problem is with you math values. Good luck.Dec 9, 2013 at 2:12
-
Hi @shellter, Thanks for the input. With that enabled this is the output: date +»%s») -$(stat -c «%Y» $SENDDIR/$NIGHTLY_FILE) date +»%s» ++ date +%s stat -c «%Y» $SENDDIR/$NIGHTLY_FILE) stat -c «%Y» $SENDDIR/$NIGHTLY_FILE ++ stat -c %Y /senddir/nightly_file.xml stat: cannot stat /senddir/nightly_file.xml’: No such file or directory ./file.sh: line 75: 1386560712 — : syntax error: operand expected (error token is «- «) I’ve looked it over but am not sure how to interpret the output. edit: Apologies about the lack of formatting.
Dec 9, 2013 at 3:52
-
Does
/senddir/nightly_file.xmlexist? The error seems to indicate otherwise.Dec 9, 2013 at 4:41
-
Hi Michael. Bingo. The rudimentary script was fine. A particular directory the script relies on was not where it should have been on the new server. Thank you both for the input!
Dec 9, 2013 at 22:48
Could anyone point out what’s wrong with the below line from a bash script I’m using?
if [ "$(( $(date +"%s")-$(stat -c "%Y" $SENDDIR/$NIGHTLY_FILE) ))" -gt "3600" ]; then
I receive the error noted in the subject line.
I had copied the script from one server to another thinking that it should work but alas, no.
- bash
- operand
![]()
Josh Crozier
228k54 gold badges386 silver badges301 bronze badges
asked Dec 9, 2013 at 1:45
PeteJPeteJ
13 bronze badges
5
-
wrap that line with
set -vxabove andset +vxbelow so you can see how the variables are being expanded. Then it should be very easy to see where the problem is with you math values. Good luck.Dec 9, 2013 at 2:12
-
Hi @shellter, Thanks for the input. With that enabled this is the output: date +»%s») -$(stat -c «%Y» $SENDDIR/$NIGHTLY_FILE) date +»%s» ++ date +%s stat -c «%Y» $SENDDIR/$NIGHTLY_FILE) stat -c «%Y» $SENDDIR/$NIGHTLY_FILE ++ stat -c %Y /senddir/nightly_file.xml stat: cannot stat /senddir/nightly_file.xml’: No such file or directory ./file.sh: line 75: 1386560712 — : syntax error: operand expected (error token is «- «) I’ve looked it over but am not sure how to interpret the output. edit: Apologies about the lack of formatting.
Dec 9, 2013 at 3:52
-
Does
/senddir/nightly_file.xmlexist? The error seems to indicate otherwise.Dec 9, 2013 at 4:41
-
Hi Michael. Bingo. The rudimentary script was fine. A particular directory the script relies on was not where it should have been on the new server. Thank you both for the input!
Dec 9, 2013 at 22:48
- Печать
Страницы: [1] 2 Все Вниз
Тема: Помогите с синтаксисом в другом скрипте (Прочитано 2691 раз)
0 Пользователей и 1 Гость просматривают эту тему.

ShadowUser15
команда от рута:
root…# echo $(($(dd if=/dev/port bs=1 skip=889 count=1 2>/dev/null) & (1 << 6)))
bash: & (1 << 6): ошибка синтаксиса: ожидается операнд (ошибочная метка « & (1 << 6)»)
проблема там где /ошибочная метка «/ — сожран символ, но при редактировании поста виден нормально, для точности см. скрин в документе
проявляется ТОЛЬКО в одном положении — «0» пишет исправно.
// = примечание,
UPD!:
a=$(dd if=/dev/port bs=1 skip=889 count=1 2>/dev/null)работает
echo $aa=$(dd if=/dev/port bs=1 skip=889 count=1 2>/dev/null)+128тоже работает
echo $a
на выходе получается строка «?+128» (без кавычек)
а воспринята как строка
заключите выражение в $(()) или $[].
вот тут-то и засада! Не пашет. Говорит ошибка синтаксиса.
внешний вид (скриншот) https://forum.ubuntu.ru/index.php?action=dlattach;topic=250560.0;attach=35744
подробно зачем оно https://forum.ubuntu.ru/index.php?topic=250560.msg1984965#msg1984965
« Последнее редактирование: 19 Октября 2014, 10:01:33 от ShadowUser15 »
вероятное введение нестандартных десятичностей, внутри системы — заставляет задуматься о переходе на другой

.ubuntufan

ShadowUser15
подозреваю что чтото& является командой, которая ждёт не то что в неё передаётся, а когда бит — всё нормально.
может глюк парсера команд баша? или надо как-то экранировать вложенный $(дд)? или там ещё пары скобок не хватает?
« Последнее редактирование: 15 Октября 2014, 22:04:12 от ShadowUser15 »
вероятное введение нестандартных десятичностей, внутри системы — заставляет задуматься о переходе на другой

.ubuntufan
ну а что именно делает «2>/dev/null» в конце dd я не понимаю. Точнее не понимаю при чём тут «2» — так то ясно что консольный текст дд со скоростью в игнор.
Это STDERR, он здесь не причем.
Байт то считывается нормально, побитовые операции в баше работают с числами а не с байтами как не странно, в этом все дело как мне кажется.
Чем приведенное выше решение в виде программы на C не устроило?

ShadowUser15
« Последнее редактирование: 16 Октября 2014, 23:34:19 от ShadowUser15 »
вероятное введение нестандартных десятичностей, внутри системы — заставляет задуматься о переходе на другой

.ubuntufan
Кросскомпилятор же
sudo apt-get install gcc-arm-linux-gnueabi
arm-linux-gnueabi-gcc -static -march=armv6 byteread.c -o byteread-arm
file ./byteread-arm
./byteread-arm: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, not stripped

ShadowUser15
Согласен, что создать бинарную программу для этих целей как минимум не сложнее чем скрипт (а для меня так и проще, на паскале ужеб работало, и как минимум в арм он компилится).
Но! Мне уже просто интересно — почему так странно ведёт себя баш.
« Последнее редактирование: 18 Октября 2014, 14:08:52 от ShadowUser15 »
вероятное введение нестандартных десятичностей, внутри системы — заставляет задуматься о переходе на другой

ArcFi
U+007F <control> = DELETE — управляющий символ, по сути, клавиша «Del».
Наверно тут надо делать так:
dd | {od|hexdump} | grep

ShadowUser15
Греп может заработает — но тащить 2 сравнительно тяжёлых команды (как и любые текстовые)…
Неужели нет нормального способа???
например
a=$(dd if=/dev/port bs=1 skip=889 count=1 2>/dev/null)исправно работает
echo $a
Так что вопрос: как с этой $a выполнить какую-нибудь побитовую операцию? А именно проверку на установленность бита.
« Последнее редактирование: 18 Октября 2014, 20:44:45 от ShadowUser15 »
вероятное введение нестандартных десятичностей, внутри системы — заставляет задуматься о переходе на другой

ArcFi
echo -en 'abcx7fdef' | od -An -to1 -N1 -j3 | sed 's/0/000/g;s/1/001/g;s/2/010/g;s/3/011/g;s/4/100/g;s/5/101/g;s/6/110/g;s/7/111/g' | grep -qE '1[0-1]{5}$' && echo OK || echo FAIL
« Последнее редактирование: 18 Октября 2014, 19:56:49 от ArcFi »

ShadowUser15
od/grep в данном случае костыль. Странный и неестественный.
вероятное введение нестандартных десятичностей, внутри системы — заставляет задуматься о переходе на другой

ArcFi
Вы же сами просили рабочее решение на Bash.
Вот и оно.
Bash не предполагает сведение зависимостей к минимуму.
Нужен минимализм — используйте какой-нибудь C.

victor00000
ShadowUser15,
всё функция.
ls /usr/bin/

ShadowUser15
Блин!
a=$(dd if=/dev/port bs=1 skip=889 count=1 2>/dev/null)|echo $aНЕ РАБОТАЕТ
а вот
a=$(dd if=/dev/port bs=1 skip=889 count=1 2>/dev/null)работает… странно, точнее не странно, но всё равно
echo $a
потом с этой а ничего не сделать 
самое весёлое что
a=$(dd if=/dev/port bs=1 skip=889 count=1 2>/dev/null)+128тоже работает
echo $a
на выходе получается строка «?+128» (без кавычек)
« Последнее редактирование: 18 Октября 2014, 21:53:39 от ShadowUser15 »
вероятное введение нестандартных десятичностей, внутри системы — заставляет задуматься о переходе на другой

ArcFi
Первый вариант бессмысленен чуть менее, чем полностью.
В третьем варианте намёк на выполнение арифметической операции над несовместимыми типами данных.
- Печать
Страницы: [1] 2 Все Вверх