1

For a shell script the variable is a table name.

If the table name is test then execute

while IFS=',' read a; do drop.sh $a; done < abc

or else execute 

while IFS=',' read a; do create.sh $a; done < abc

How can i achieve this in Linux

1

2 Answers 2

1

You can use an if - else statement like this;

if [ test ]
then
  do something
else
  do something else
fi  

In your case

if [ "$yourVariable" = "test" ]
then
  while IFS=','...
else
  while IFS=','...
fi

See here for more, it is bash specific, but mostly usable for other linux shells.

Edit As suggested in the comments, here are some other resources:

Sign up to request clarification or add additional context in comments.

1 Comment

Please don't point folks at the ABS -- it's rather infamous for promoting bad practices in its examples. The BashGuide is a much more effectively-maintained introduction (with its relevant page here), as is the bash-hackers wiki, which covers test here. By contrast, the POSIX spec for test will apply to all POSIX shells.
1

For multiple conditions of the same type, you can also use the case:

for table in test bla something; do

    case "$table" in
        test)
            echo actions for $table
            ;;
        bla)
            echo another actions for $table
            ;;
        *)
            echo actions for anything other as test or bla eg for $table
            ;;
    esac

done

output from the above

actions for test
another actions for bla
actions for anything other as test or bla eg for something

e.g. for you case, you could write

table="test"

while IFS=, read -r a; do
        case "$table" in
                test) drop.sh "$a"  ;;
                *)  create.sh "$a"  ;;
        esac
done < abc

Some notes:

  • always use read -r unless you know why don't want the -r
  • always quote the variables like "$a"
  • You while read-ing the same abc for the both cases, so DRY (don't repeat yourself) and move the while read... outside of your condition...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.