Shell scripts are useful, aren't they? But ** I forget how to write it ** I'm lazy to ask Google teacher for each command, so I will summarize how to write it.
Shell art is not a level article, but well, it is a useful article if you write a small script. maybe.
It's an offbeat and messy entry, but don't worry.
From now on, I will write about the basic syntax of shell scripts. Seriously basic syntax.
It's easy to forget, but let's give authority first. If you don't, you can't do it.
(*'~') $ touch sample.sh
(*'~') $ chmod +x ./sample.sh
Don't forget to put the declaration on the first line * this is a shell script *. There is no difference between sh and bash, so bash seems to be fine.
#!/bin/bash
You want to know the comment out. Well, just add #
to the beginning.
(*'~') $ cat sample.sh
#!/bin/bash
# echo "not display"
echo "display"
(*'~') $ ./sample.sh
display
I will summarize how to write arguments and variables
It's quick to actually write and remember. You can take arguments like the script below. It means that it is a variable with $
.
(*'~') $ cat sample.sh
#!/bin/bash
echo $#
echo $@
echo "$@"
echo $*
echo "$*"
echo $0
echo $1
echo $2
echo $9
If you give an argument as a trial, it will look like this.
(*'~') $ ./sample.sh hoge 1
2
hoge 1
hoge 1
hoge 1
hoge 1
./sample.sh
hoge
1
(*'~') $
Description
--$ #
: Number of arguments passed to the script
--$ @
: All arguments passed to the script.
--$ *
: All arguments passed to the script. Same as $ @
unless enclosed in double quotes.
-- $ 0
: The name of the script being executed.
-- $ n
: The nth argument passed to the script. An empty string is output when the range of the given argument is exceeded. The 10th and subsequent arguments cannot be expanded unless they are described as $ {n}
.
$ @
and $ *
If you do not enclose it in double quotes, it is the same as $ @
, but if you enclose it, there is a difference between outputting for each argument or outputting with one argument collectively as shown below.
(*'~') $ cat sample.sh
#!/bin/bash
for arg in "$@"
do
echo $arg
done
for arg in "$*"
do
echo $arg
done
(*'~') $ ./sample.sh hoge foo 1
hoge
foo
1
hoge foo 1
I've already written it as an argument, but when I refer to a variable, $
is added. Do not add when defining.
Also, note that ** do not put spaces to the left and right of =
when defining variables **. Let's connect and write.
(*'~') $ cat ./sample.sh
#!/bin/bash
hoge="This is Hoge"
echo $hoge
(*'~') $ ./sample.sh
This is Hoge
Expand variables in a string. Just write variables in double quotes.
(*'~') $ cat ./sample.sh
#!/bin/bash
hoge="This is Hoge"
echo "What is this? $hoge"
(*'~') $ ./sample.sh
What is this? This is Hoge
The command execution result can be stored in a variable. Describe the command you want to execute in $ ()
.
(*'~') $ cat ./sample.sh
#!/bin/bash
hoge=$(echo "This is Hoge")
echo "What is this? $hoge"
(*'~') $ ./sample.sh
What is this? This is Hoge
As I wrote a little earlier, I will describe how to write if, for, and case statements.
I will definitely use it. I have a little habit.
There are comparison operators such as -n
and -z
, but it's quite confusing, so I think it's easier to make comparisons like " "=" $ hoge "
.
By the way, ** Be sure to enclose variables in double coats and compare them as strings **
(*'~') $ cat sample.sh
#!/bin/bash
if [ "" = "$1" ]; then
echo "params not given"
fi
if [ "" != "$1" ]; then
echo "params given $1"
fi
(*'~') $ ./sample.sh
params not given
(*'~') $ ./sample.sh hoge
params given hoge
if ~ else
This is another frequently used syntax. I forget ʻelif` every time.
(*'~') $ cat sample.sh
#!/bin/bash
if [ "hoge" = "$1" ]; then
echo "params is hoge"
elif [ "foo" = "$1" ]; then
echo "params is foo"
else
echo "params are not hoge or foo"
fi
(*'~') $ ./sample.sh hoge
params is hoge
(*'~') $ ./sample.sh foo
params is foo
(*'~') $ ./sample.sh other
params are not hoge or foo
for
statementI did a little bit earlier
(*'~') $ cat sample.sh
#!/bin/bash
for arg in "$@"
do
echo $arg
done
(*'~') $ ./sample.sh hoge foo 1
hoge
foo
1
case
statementI use it soberly
(*'~') $ cat sample.sh
#!/bin/bash
case "$1" in
hoge)
echo "params is hoge"
;;
foo)
echo "params is foo"
;;
*)
echo "params are not hoge or foo"
;;
esac
(*'~') $ ./sample.sh hoge
params is hoge
(*'~') $ ./sample.sh foo
params is foo
(*'~') $ ./sample.sh other
params are not hoge or foo
Here document that is sober and convenient. Of course you can use it in the shell. If you enclose the first EOS part in single quotes, variable expansion will not occur. By the way, EOS can be anything as long as the first and last characters are aligned. As a convention, if all capital letters.
(*'~') $ cat sample.sh
#!/bin/bash
cat << EOS
you can discribe multiple lines.
$1
EOS
cat << 'EOS'
$1
EOS
(*'~') $ ./sample.sh hoge
you can discribe multiple lines.
hoge
$1
Give a number to the exit command to end the script prematurely. 0
is normal termination and 1
is abnormal termination. It is mainly used to stop processing when there are not enough arguments.
(*'~') $ cat sample.sh
#!/bin/bash
case "$1" in
0)
echo "success"
exit 0
;;
*)
echo "error"
exit 1
;;
esac
echo "can't reach"
(*'~') $ ./sample.sh 0
success
(*'~') $ ./sample.sh 1
error
How about something like this?
I didn't find an entry that was systematically described (probably not easy to find), so I wrote it. After all, if you write it yourself, you will feel like you understand something.
We will add an advanced version at a later date.
Recommended Posts