I'm re-learning scripting and found an example the book doesn't explain very well.
e.g.:
if [ ! $# -eq 0 ]
This is testing to determine if '$#' does not equal zero, yes?
But what then is the value of '$#'?
Are there others?
'##', '#?' ?
Thank you
$# is the number of the arguments you passed to your script.
For example, your have script called a.sh,
#!/bin/bash
echo $#
And you run it like
/bin/bash a.sh 1 2 3
you will get 3.
There are others like $#.
Related
All I want to do is perform a simple equivalency check like you would in other languages with the == operator. It would look something like
if $var == 0
echo Hello world!
end
I'm a really surprised that the closest thing in the docs I could find is contains which allows for this silly makeshift solution
if contains $var 0
echo Hello world!
end
Use the test builtin, like
if test "$var" = 0
# do stuff
end
test is also available as [, in which case it expects the last argument to be ], so you can write
if [ "$var" = 0 ]
# do stuff
end
I'm quoting the variable here because tests argument parsing (which was taken straight from POSIX) doesn't work well with fish's lists, so if $var doesn't have exactly one element it will break with surprising errors.
In the fish shell the syntax for conditionals is hard to find. Does anyone have a link to an explanation of how to write an if, with ands and ors?
In particular I would like to write
if not $status
do a command
end
To do a command when the previous command returned a non-success. How do I do that?
See http://fishshell.com/docs/current/commands.html#if and http://fishshell.com/docs/current/tutorial.html#tut_conditionals.
Fish's if structure looks like this:
if COMMAND
# do something if it succeeded
else
# do something if it failed ($status != 0)
end
Then there are also the not, and and or commands, that you can use like
if not COMMAND1; or COMMAND2
# and so on
If you really want to test a variable (e.g. $status), you need to use test as the command, like
if test $status -eq 0
Do keep in mind that $status changes after every command, so if you need to use the status of an earlier command (common in prompts) you need to do what Joachim Pileborg said, save it to another variable.
Also, test has some issues with quotes (because it's one of the few parts of fish to adhere to POSIX) - if in test $foo -eq 0 $foo is undefined, test will error out, and if it is undefined in test -n $foo, the test will be true (because POSIX demands that test with one argument be true).
As a sidenote, in fish versions before 2.3.0, you need to add begin and end around a condition with and or or because it was interpreted weirdly.
So you'd have to do
if begin COMMAND; or COMMAND2; end
# do something for status = 0
The shortest short form would be
the_previous_command; or do_a_command
# ..................^^^^^
Assuming you get your $status from "the_previous_command"
I use the status variable to display it on the prompt if it's non-zero. For that I use the following function:
function __pileon_status_prompt
set -l __status $status
if test $__status != 0
printf '[%s%s%s]' (set_color red) $__status (set_color normal)
end
end
As you can see I set a local variable to the value of $status and the check that variable in the condition.
Using fish shell, I'm writing very simple script that checks the command execution
#!/usr/bin/fish
command
if $status
echo "Oops error"
else
echo "Worked OK"
#...
end
And get the error message:
fish: Variables may not be used as commands. Instead, define a function like “function status; 0 $argv; end”. See the help section for the function command by typing “help function”.
The message looks pretty straight forward but no "defining function like..." nor "help function" helps solving the problem.
There is also a 'test' command, that sounds promising. But docs say it is to be used to check files...
How this simple thing should be done with fish shell?
Heh... And why all documentation is SO misleading?..
P.S. Please, don't write about 'and' command.
Fish's test command currently works exactly like POSIX test (i.e. the one you'll find in bash or similar shells). It has a couple of operations, including "-gt", "-eq", "-lt" to check if a number is bigger, equal or less than another number, respectively.
So if you want to use test, you'll do if test $status -eq 0 (a 0 traditionally denotes success). Otherwise, you can check the return value of a command by putting it in the if clause directly like if command (which will be true if the command returns 0) - that's what fish is trying to do here, which is why it complains about a variable being used in place of a command.
I am working on an sh script in which I am in a WHILE loop where a variable gets incremented every iteration, and I'd like to save a file every five iterations.
What I'd normally do (say in C) would be to do an if ( n % 5 == 0) test, but I don't know whether that's possible here..? If it isn't, does anyone have any ideas that would get the job done?
Cheers!
If your sh really is sh and not just bash being run as sh then this will work just fine
if [ `expr $n % 5` -eq 0 ]
then
# do something
fi
If your sh is really bash then put your test in (( )) like so
if (( $n % 5 == 0 ))
then
# do something
fi
You should use bc when doing math in shell
if [ `echo "3 % 2" | bc` -eq 0 ]
Notes on the above answers
Backticks are considered deprecated by now (many further notes), so I advise on switching to $(( ... )) or $( ... ) constructs.
Both expr (man page), and bc (man page) are external commands, and as such would cause some measurable slowdown in more complex examples than mine or those above. Furthermore, there already is a way to easily avoid them (both). Also, they might not be available on all systems by default for instance.
Suggested new solution
(Might be imperfect under certain conditions, I did test just basic scenarios.)
The simplest possible while portable and POSIX-ly correct code (no Bashisms (Greg's Wiki on how to re-write Bash snippets to POSIX); (Wikipedia overview on POSIX)) to test oddity (as an example) of a number would be as follows:
#!/bin/sh
n=10
if [ $(( n % 2 )) -eq 0 ]; then
printf '%s\n' "Number $n is Even"
else
printf '%s\n' "Number $n is Odd"
fi
You can, of course, test any modulo there, not just if a number is even / odd.
This question is based on this thread.
The code
function man()
{
man "$1" > /tmp/manual; less /tmp/manual
}
Problem: if I use even one option, the command does not know where is the wanted-manual
For instance,
man -k find
gives me an error, since the reference is wrong. The command reads -k as the manual.
My attempt to solve the problem in pseudo-code
if no parameters
run the initial code
if one parameter
run: man "$2"
...
In other words, we need to add an option-check to the beginning such that
Pseudo-code
man $optional-option(s) "$n" > /tmp/manual; less /tmp/manual
where $n
n=1 if zero options
n=2 if 1 option
n=3 if 2 options
....
How can you make such an "option-check" that you can alter the value of $n?
Developed Problem: to make two if loops for the situations from n=1 to n=2
How about passing all the arguments
function man()
{
man $# > /tmp/manual; less /tmp/manual
}
What is the bug in less which you mention in the title?
First, you can pass all of your function's arguments to man by using $* or $#. You can read man sh for the precise details on the difference between the two; short story is to almost always use "$#" with double quotes.
Second, the temporary file is unnecessary. You could make this a little cleaner by piping the output of man directly to less:
function man() {
man "$#" | less
}
By the way, if you're just trying to use a different pager (man uses more and you want the fancier less) there's a commonly recognized PAGER environment variable that you can set to override the default pager. You could add this to your ~/.bashrc for instance to tell all programs to use less when displaying multiple screens of output:
export PAGER=less
To answer your precise question, you can check the number of arguments with $#:
if [ $# -eq 0 ]; then
: # No arguments
elif [ $# -eq 1 ]; then
: # One argument
# etc.
You might also find the shift command helpful. It renames $2 to $1, $3 to $2, and so on. It is often used in a loop to process command-line arguments one by one:
while [ $# -gt 1 ]; do
echo "Next argument is: $1"
shift
done
echo "Last argument is: $1"