Condition in bash script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Condition in bash script
# 1  
Old 09-13-2019
Condition in bash script

I want get [4 file name string] from user and pass these parameters to bash script. script should copy files in user home directory.

FYI: each file might be exist or not, might be one of them exist or four of them.

Here is my script, it always copy file1 and seems only one of them [if] execute!

Code:
#!/bin/bash  
for var in "$@" ; do
    T1=$1
    T2=$2
    if [ $T1 = "File1" ]; then
        cp file1 /home/users
    else      
        if [ $T2 = "File2" ]; then
            cp file2 /home/users
        ....
        fi
    fi 
done

# 2  
Old 09-13-2019
This may help you figure out your logic:

In your 'for' loop:

"var" is going to start with arg1 and get reassigned to the next in the list each iteration until argn
"T1" is always going to be arg1
"T2" is always going to be arg2

Example:

Code:
$ cat /tmp/tryit
#!/usr/bin/bash

for a in "$@"; do
  echo $a $1 $2
done
$
$
$ /tmp/tryit a b c d e f
a a b
b a b
c a b
d a b
e a b
f a b

This User Gave Thanks to eckertd For This Post:
# 3  
Old 09-13-2019
Exactly, I get parameter with correct order , but seems if (s) had problems!
Do you have idea about that part of code?

Quote:
Originally Posted by eckertd
This may help you figure out your logic:

In your 'for' loop:

"var" is going to start with arg1 and get reassigned to the next in the list each iteration until argn
"T1" is always going to be arg1
"T2" is always going to be arg2

Example:

Code:
$ cat /tmp/tryit
#!/usr/bin/bash

for a in "$@"; do
  echo $a $1 $2
done
$
$
$ /tmp/tryit a b c d e f
a a b
b a b
c a b
d a b
e a b
f a b

# 4  
Old 09-13-2019
If you're looking to see if the specified file exists

Code:
if [ -f <filename> ]; then
  cp ....
else
  cp ...
fi

'-f' test if the file exists, and is a regular file (not a directory, etc)

See 'man test' for more conditions you can test for. They come in handy!
# 5  
Old 09-13-2019
Before cp I cleanup /home/user , and I make sure destination is empty after that run script , do you have any other recommendation?

Quote:
Originally Posted by eckertd
If you're looking to see if the specified file exists

Code:
if [ -f <filename> ]; then
  cp ....
else
  cp ...
fi

'-f' test if the file exists, and is a regular file (not a directory, etc)

See 'man test' for more conditions you can test for. They come in handy!
# 6  
Old 09-13-2019
You lost me there...can you post the entire script, along with how you intend for it to be called?

You could also add a "set -x" line before your 'if' statement so you can see what values it's comparing to see if it's what you expect.
# 7  
Old 09-13-2019
Sorry about confusing you 😄
After these issues I try to export this part in new and separate script, and just cleanup destinations with rm command and then run if(s)


Code:
#!/bin/bash  
rm -rf /home/users
for var in "$@" ; do
    T1=$1
    T2=$2
    if [ $T1 = "File1" ]; then
        cp file1 /home/users
    else      
        if [ $T2 = "File2" ]; then
            cp file2 /home/users
        ....
        fi
    fi 
done

Quote:
Originally Posted by eckertd
You lost me there...can you post the entire script, along with how you intend for it to be called?

You could also add a "set -x" line before your 'if' statement so you can see what values it's comparing to see if it's what you expect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/bash one liner replacement for a if condition

Hi. I wrote this small bash script, i want to compare second column from file1 with file2 if a pattern matches. Files are small and I am sure that pattern occurs only once. I think this can be rewritten into a awk one liner. Appreciate if someone could give me idea. Whole NR FNR confuse me :o ... (6 Replies)
Discussion started by: ctrld
6 Replies

2. Shell Programming and Scripting

Creating a condition on a bash script

I wrote a code to find codons in a DNA string. The only problem I have is how do I make the code only work for a file with DNA. This means the file only has the characters a,c,g,t and no white space characters. (3 Replies)
Discussion started by: germany1517
3 Replies

3. Shell Programming and Scripting

Need Multiple checks inside if condition in a bash shell script

Hi, I need to perform the untar and rm operation if the file found is a .tar and does not have test.tar or hello.tar as the file names. Below is the loop to check the same. for tf in *.tar do if ] then found=1 ... (1 Reply)
Discussion started by: mohtashims
1 Replies

4. Shell Programming and Scripting

Bash script if condition not executing

issue is with .txt files (7 Replies)
Discussion started by: anil529
7 Replies

5. Shell Programming and Scripting

Add another condition to bash for when not met

In the below I can not seem to add a line that will add Not low if the statement in bold is not true or meet. I guess when the first if statement is true/meet then print low, otherwise print Not low in $(NF + 1). I am not sure how to correctly add this. Thank you :). if(low <= $2 && $2 <=... (5 Replies)
Discussion started by: cmccabe
5 Replies

6. UNIX for Dummies Questions & Answers

Bash condition test at the end of string

I want to check (using bash condition test function) if string contains three spaces, ignoring last three spaces at the end of string. string_to_report='foo bar foo bar ' string_to_ignore='foo bar ' (8 Replies)
Discussion started by: useretail
8 Replies

7. Red Hat

how to use if condition with sed command in BASH

Urgent help with bash scripting 1- i am using grep to find a string called: tinker panic 0 in a file /etc/ntp.conf if the string is not there, i want to add the strings in /etc/ntp.conf file in the first line of the file. if not do nothing or exit. 2- also i want to add # in front of the... (2 Replies)
Discussion started by: lamoul
2 Replies

8. Programming

if condition in bash

Hi, I have meaning to include an if condition statement in my code to check the directory for existing output files and if its existing i want the program to delete it before doing the succeeding command. i just dont know the correct syntax for it. thanks much guys, this forum has indeed been very... (4 Replies)
Discussion started by: ida1215
4 Replies

9. Shell Programming and Scripting

Multiple condition checking in bash

Hi All, I am trying to check if two variables have value assigned to it. i am doing it like if ] then echo "Please specify either single hostname or host file for the report" usage exit fi But its not working for it.Even i specify values for both variables it dont go... (6 Replies)
Discussion started by: kailash19
6 Replies

10. Shell Programming and Scripting

Bash: if condition as variable

How can I use a variable that has the conditions for the if statement stored in it? my test script condition=" || || " if "$condition" then echo "true" else echo "false" fi output $ ./test2.sh ./test2.sh: line 3: || || : command not found false (2 Replies)
Discussion started by: curlee2002
2 Replies
Login or Register to Ask a Question