the idea is to create script with menu and when option 1 or2 is pressed program should clear screan display info and get back to menu....
I managed some code but getting errors...
#!/bin/bash
choice1=ls -l|awk'{print $9 $1}'
choice2= ls | wc -c
choice3=exit
while [ $choice -ne 2 ]
do
clear
echo " ========================================= "
echo " = 1) display diles and permissions only = "
echo " = 2) calculate numbers of bytes = "
echo " = 3) exit = "
echo " ========================================= "
echo -n " choice: "
read choice
case "$choice" in
1 ) echo " $choice1 " # how to display info and get back to menu script
begining?
2 ) echo " $choice2 " # same as with number1
3 ) exit
Assigning the output of a command to a variable needs the command(s) on the right side of the = either enclosed inside backticks `command` or this $(command) .
There may be no space between the = and the value or command of a variable.
If you start a while-loop, you got to close the "do" with "done".
Same for case - you got to close it with "esac".
Single entries for case are ended with double semicolon ;; .
After this, try again and check your errors, read them and try to fix them. Some error messages just explain what is going wrong.
Also have a look at a scripting introduction/guide so you get some basic knowledge.
No, you missed setting the ;; at the end of each case option.
Also when getting error messages, start posting them so people don't have to guess around, thanks.
If you use vi for example, you can do
:set number
So you and people reading this don't have to count lines. There is also the set -x to turn debugging output on and set +x to turn it off.
will try do it later, but a fix in a code would appreciate.
---------- Post updated at 06:39 PM ---------- Previous update was at 02:31 PM ----------
ok got it almost working
no errors these time but if I chose both option 1&2 there is no message displayed...
if I chose 3 - then script exits so its fine, but to display 1&2 ?
#!/bin/bash
choice=0
c1=$(ls -l|awk '{print $9 $1}')
c2=$(ls | wc -c)
c3=exit
while [ $choice -ne 3 ]
do
clear
echo " ========================================= "
echo " = 1) display diles and permissions only = "
echo " = 2) calculate numbers of bytes = "
echo " = 3) exit = "
echo " ========================================= "
echo -n " enter a number : "
read number
case $number in
1 ) echo 2 $c1 " ;; # these options are not shown
2 ) echo " $c2 " ;; # and here....
3 ) exit ;;
esac
done
As said already, please post an error message, if you get one, thanks. Script is working fine though the output is not very useful in my eyes.
As said already, have a look into basic shell scripting guides too, thanks. You can learn tons out of them.
the "2" instead of " " " was typo, does it work for you? strange because it doesnt display output of
choice1=$(ls -l|awk '{print $9 $1}')
or
choice2=$(ls | wc -c)
it just exits when I enter " 3 "
I read online tutorials and got a Advanced Bash-Scripting Guide but I need a while with scripts to be on the level when I look at the code and find errors. Just now I am on the errors making level . . .
I am pasting full script, can you tell me if that works for as it should,
#!/bin/bash
choice=0
choice1=$(ls -l|awk '{print $9 $1}')
choice2=$(ls | wc -c)
choice3=exit
while [ $choice -ne 3 ]
do
clear
echo " ========================================= "
echo " = 1) display diles and permissions only = "
echo " = 2) calculate numbers of bytes = "
echo " = 3) exit = "
echo " ========================================= "
echo -n " enter a number : "
read choice
case "$choice" in
1 ) echo " $choice1 " ;;
2 ) echo " $choice2 " ;;
3 ) exit ;;
esac
done
It works but your clear just wipes the screen so you can't see it. Put it in the previous line of your while -loop. Indention of code will be helpful generally.