[bash] variables and if conditions


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users [bash] variables and if conditions
# 1  
Old 11-16-2011
[bash] variables and if conditions

Hello. Sorry for the stupid question... Here is the problem. I'm trying to do a script that offers options for backup the root partition in unix sistems.

Here is the code part that i am talking of.


Code:
echo "tar can archive your file using gzip or Bzip2"     
echo "gzip : relatively poor compresion ratio and high speed"     
echo "Bzip2 : better compression ratio and slow speed"     
echo "Choose now : gzip(z) or Bzip2(j)"     
read archive_type          
if archive_type=z; then          
archive_type_0="gz"          
fi                    

if archive_type=j; then          
archive_type_0="bz2"          
fi                           

echo "Type the path for backup image(don't type the final slash in the patch)"     
echo "Oh, and be sure that your current user level have read/write acces on that path"     
read path_save          
echo "Enter the name of the backup file"      
read name_save     
echo "The file will be saved at "$path_save"/"$name_save".tar."$archive_type_0""     
sleep 5  
tar -cvp"$archive_type"f"$path_save"/"$name_save".tar."$archive_type_0" \
--exclude=$path_save/$name_save \
--exclude=/proc --exclude=/lost+found  \
--exclude=/sys \
--exclude=/mnt \
--exclude=/media \
--exclude=/dev  /

This is the code part that really bothers me. Leaving the variables without reference call ($) in if statement, seems to work. However, when adding "$"

Code:
if archive_type=z; then         
 $archive_type_0="gz"          
fi                    
if archive_type=j
then          
   $archive_type_0="bz2"          
fi

the terminal says "/home/editheraven/Desktop/rootsave: 57: j=z: not found" the same for "j" variable. Anyway, the code seems to work without reference call.

Another thing. How can i do a while loop like this
Code:
 
[pseudocode]  
function select 
{ read archive_type 
if archive_type=j or archive_type=z 
break 
else call function select

I see that it's a little tricky to pass funtion return value, but i don't see any easy way to verify that the entered key is j or z and if not, to return at "read archive_type".

I know that it sounds a little silly, but it's kinda different from c code, where this can easely be done.

Last edited by vbe; 11-16-2011 at 10:33 AM.. Reason: formatting for ease of reading...
# 2  
Old 11-16-2011
Should this not be:
Code:
if ( $archive_type=z ); then         
   archive_type_0="gz"          
fi                    
if ( $archive_type=j )
then          
    archive_type_0="bz2"          
fi

In other words look where you have placed the $ sign...
# 3  
Old 11-16-2011
Even like this, the output is the same.
# 4  
Old 11-16-2011
Can you run using ksh? In which case I can correct the script and let you try...
# 5  
Old 11-16-2011
for ksh:
Code:
echo "tar can archive your file using gzip or Bzip2"
echo "gzip : relatively poor compresion ratio and high speed"
echo "tar can archive your file using gzip or Bzip2"
echo "gzip : relatively poor compresion ratio and high speed"
echo "Bzip2 : better compression ratio and slow speed"
echo "Choose now : gzip(z) or Bzip2(j)"
read archive_type

if [ "$archive_type" = "z" ]
then
   archive_type_0="gz"
fi

if [ "$archive_type" = "j" ]
then
   archive_type_0="bz2"
fi

echo "Type the path for backup image(don't type the final slash in the patch)"
echo "Oh, and be sure that your current user level have read/write acces on that path"

read path_save
echo "Enter the name of the backup file"
read name_save
echo "The file will be saved at "$path_save"/"$name_save".tar."$archive_type_0""
sleep 5
tar -cvp"$archive_type"f"$path_save"/"$name_save".tar."$archive_type_0" \
--exclude=$path_save/$name_save \
--exclude=/proc --exclude=/lost+found  \
--exclude=/sys \
--exclude=/mnt \
--exclude=/media \
--exclude=/dev  /

# 6  
Old 11-16-2011
Had time to test:
Code:
#!/usr/bin/bash
echo "Choose now : gzip(z) or Bzip2(j)"
read archive_type

if [ "$archive_type" = "z" ]
then
   archive_type_0="gz"
fi

if [ "$archive_type" = "j" ]
then
   archive_type_0="bz2"
fi
echo archive_type_0 " : " $archive_type_0
#
n12:/home/vbe $ ./004
Choose now : gzip(z) or Bzip2(j)
z
archive_type_0  :  gz

So works... also in bash...
This User Gave Thanks to vbe For This Post:
# 7  
Old 11-16-2011
Thanks a lot. It works now.

note : for me, bash is located at

Code:
#!/bin/bash

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to assign points to variables based on conditions and update specific field

I have been reading old posts and trying to come up with a solution for the below: Use a tab-delimited input file to assign point to variables that are used to update a specific field, Rank. I really couldn't find too much in the way of assigning points to variable, but made an attempt at an awk... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Bash: How to use read with conditions & loops

Hello, Below I try to control that the input is good an IP : #!/bin/bash cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt chmod 644 /home/scripts/interfaces.txt echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below... (9 Replies)
Discussion started by: Arnaudh78
9 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

Expect - bash and variables

I was wondering if anyone could provide some assistance. I trying to run an expect script within bash and get the results of a variable called RESULT. I Have tried a few things but none of them have worked. I know that the child process (the expect script) in this instance cannot set a variable... (6 Replies)
Discussion started by: ylafont
6 Replies

5. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

6. Shell Programming and Scripting

functions and variables in bash

I have a bash script with some functions as below and am wondering if I can use the variables declared in setup in the other functions and in the rest of the bash script. setup(){ none=0; low=1; medium=2; high=3; debug=4 var1="red" var2="fred" } create_basemap() { ... (7 Replies)
Discussion started by: kristinu
7 Replies

7. Shell Programming and Scripting

while loops and variables under bash

Hi, This is probably going to be very simple but i came across something i can't quite explain. Here is the situation: i have a list of files, which i'd like to process one by one (get the size, make some tests, whatever) and generate some statistics using different variables. Something... (5 Replies)
Discussion started by: m69w
5 Replies

8. Shell Programming and Scripting

Bash variables

Ummm can anybody help me with this one? Its prob quite simple. I bascially have a file name say J1x2x3x7.dat Im using the file name as a variable in a bash script. Want I want to do is extract most of the file name and make it a new variable expect with say one of the number now a... (2 Replies)
Discussion started by: RichieFondel
2 Replies

9. UNIX for Dummies Questions & Answers

bash shell variables

Hi everyone, I have added this to my .bash_profile. Whenever I log in and when I type javac I get a error message (java: command not found). Does the order counts? PATH=$JAVA_HOME/bin:$PATH:$HOME/bin JAVA_HOME=$JAVA_HOME/usr/local/jdk1.3.1_02 export JAVA_HOME PATH Thanks ny (3 Replies)
Discussion started by: xNYx
3 Replies
Login or Register to Ask a Question