Good day!
I use openSuse 12.1.
I had wrote this script:
#!/bin/bash
LOG_DIR=/var/log
ROOT_UID=0
E_NOTROOT=67
array=(This is New Array)
date -u +%s
id
$id = id -u
echo $id
$variable = date -u +%s
echo $varialbe
if "$UID" -ne "$ROOT_UID" ]
then
echo "You must have root privileges"
exit $E_NOTROOT
fi
if -n "$1" ]
then
$lines=$0
fi
if lines != NULL ]
then
echo $lines;
fi
read input
echo $input
for line in ${array@]}
do
printf " " $item
done
case $input in
"E" | "e")
echo "You select e"
if ! -d /path/directory/ ]; then
f=$(mkdir -p /home/vladimir/Downloads/siteArchive);
echo f;
if f -ne 0];
then
echo "Dirrectory was created"
tar -cvzf webSite2.tar.gz /home/vladimir/Downloads/siteArchive/
echo "Archive was created"
else
echo "Dirrectory was not created "
else
echo "Dirrectory already exist";
fi
;;
"J" | "j")
echo "You select j"
if ! -d /path/directory/ ]; then
f=$(mkdir -p /home/vladimir/Downloads/siteArchive);
echo f;
if f -ne 0];
then
echo "Dirrectory was created";
tar -cvzf webSite2.tar.gz /home/vladimir/Downloads/siteArchive/;
echo "Archive was created";
else
echo "Dirrectory was not created ";
else
echo "Dirrectory already exist";
fi
;;
esac
and output of it is:
"
1393603928
uid=0(root) gid=0(root) groups=0(root)
./11: line 11: =: command not found
./11: line 15: =: command not found
./11: line 70: syntax error near unexpected token else' ./11: line 70: else ’
"
Correctly my questions is:
How to define dialect of shell of my system?
How correct to assign values to variables?
What is the syntax of: “if - then - else - done” and “for - in - do -done” ?
To begin with, this is not an sh (POSIX shell) script (as you mention in your thread title), but a bash (GNU Bourne-Again Shell)) script a the shebang in your script says.
Then, why isn’t the execution of your shell (prompt, command, the output and the next prompt) posted between CODE tags, like your script itself is? Please put ALL computer ooutput facts between CODE tags here.
Then about your questions:
What “dialects” do you mean?
You can assign values to v ariables in a lot of ways, but the most obvious one is
VARIABLE=value
There is no if - then - else - done construct; it is if - then - else - fi. You can find all those things in
man bash
.
In general, my advice is to first study some docs about bash programming. There is enough on the internet (googling with “learning bash” gives you abundant info). Then try small scripts to see if you understand what you are doing and do not start with a 30-40 line script wich will be full of misunderstandings when you do not learn step by step.
That’s wrong in three ways! First you take the value of $id on the left side of the ‘=’, second there should be no space before and after the ‘=’, third you have to tell bash to execute that command (“id -u”) and take the output for the assignment (I take it you want to assign the output of “id -u” to the variable $id, right?).
So it should be:
id=`id -u`
or
id=$(id -u)
That’s also true for the other assignments in your script of course.
There is no if - then - else - done construct; it is if - then - else - fi. You can find all those things in
man bash
.
In general, my advice is to first study some docs about bash programming. There is enough on the internet (googling with “learning bash” gives you abundant info). Then try small scripts to see if you understand what you are doing and do not start with a 30-40 line script wich will be full of misunderstandings when you do not learn step by step.
See here for a start regarding if-then-else: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html
“man bash” as suggested should tell you as well, or have a look at “info bash”, that’s even more detailed.
That is only true if you assume you know what the OP was wanting to code.
As he did not tell what his purpose is, we (at least I, not being clairvoyant) can only guess.
If the OP wants to call the = command with the arguments id and -u, the coding is correct. Only thing is then that there is no = command found. And that is what the message says rotfl!
Of course this is only an assumption, but what else should the code want to achieve?
And I think I made it clear that I’m just assuming the desired behaviour here.
This is the most obvious thing though in this case, no need to be a clairvoyant to come to that conclusion…
If the OP wants to call the = command with the arguments id and -u, the coding is correct. Only thing is then that there is no = command found. And that is what the message says rotfl!
???
“IF the OP wants to call the = command”? Why is there the “$id” before the ‘=’ then?
In this case “$id” is empty of course, because nothing has been assigned to it, so bash tries to run the ‘=’ command.
Why? This is an interesting discussion. I hope it will tell people to describe what they wanted, what they did, what they expected to happen and what happened when they post a question/problem here…
TBH, I don’t really find this particular discussion interesting. I just wanted to give the OP a hint how to fix his script. I don’t want to discuss now what he may or may not have had in mind when writing this script.
I agree of course that people should clarify what they actually want to achieve when they ask for help.