0

I'm not programmer and I'm trying to make my first script.

I have:

# NOMBREX1:lalalala
# NOMBREX2:0

if [ "$NOMBREX2" == "0" ]; 
    then ??????
fi
$COMMENT1 rar a -r .........

If NOMBREX2=0 I want to rename $COMMENT1 to # for commenting this line.

How can I do this?

| improve this question | | | | |
  • Simply put: You can't comment out a line in the script by changing the value of $COMMENT1. If there was a comment in the script, Bash would not even read it. If you change a string to have # at the start, it is not a comment, but still a string, just starting with #. – slhck Jun 10 '14 at 8:25
  • @slhck Strictly speaking quite true, but I still think what the OP is trying to achieve isn't really the commenting out but rather conditional execution. (Also based on the OP's first language fairly obviously not being English.) I guess we'll find out. :) – a CVn Jun 10 '14 at 8:26
  • @MichaelKjörling Yes, of course the OP is using the wrong approach altogether—your answer is great—but I was just explaining why this specific method wouldn't work. – slhck Jun 10 '14 at 8:31
  • You are right,sorry for my bad english,my question is not well builded. Thanks to both for your time,the answer two works for me. – trugulum Jun 10 '14 at 13:09
  • @user331968 Poor grasp of English is usually not a problem. As long as the intent is clear, if it bothers someone enough they will come along and fix it up. Focus on contributing constructively, to make the post as well-written as you can, and even more so to be as specific as possible about your end goal and what you have tried before asking. – a CVn Jun 10 '14 at 15:51
4

You already have all the building blocks you need, you just need to reverse the logic (and fix a small syntax error).

First off, a bit of background. [ is (basically) an alias to test. Much of this can be found (in bits and pieces) in the test(1) man page; even though learning the syntax for everything is probably not practical particularly to begin with, I recommend familiarising yourself with what test is capable of doing.

If I understand your question correctly, you want to execute the rar command if and only if $NOMBREX2 is not equal to "0".

test has a lot of conditional operators, including = ("the strings are equal") and != ("the strings are not equal"). Note that integer values use different operators including -eq and -ne respectively. Why it's done that way is beyond me, but that's water under the bridge by now.

There are two ways in bash to run a command if a statement evaluates to true, or more accurately if a command returns a zero exit status; either enclose it in an if ...; then .. fi block, or use the && operator to separate the commands. You can do the same thing if you want to execute a command when a statement evaluates to false (returns a non-zero exit status); if ! ...; then ... fi (the ! means "not") or use || between the commands.

Since we already know that the condition you want for executing the rar command is an inequality comparison, we can write that in two different ways (assuming you want string comparisons; otherwise, instead of != use -ne in the conditions):

Option 1 (idiomatic, good for simple conditions and single commands):

test "$NOMBREX2" != "0" && rar a -r ...

Option 2 (good for more complex conditions or multiple commands):

if [ "$NOMBREX2" != "0" ]; then
    rar a -r ...
    # ... anything else goes here ...
fi
| improve this answer | | | | |
  • Well,I'm at work but made a quick test with option 2 and is working perfectly.<br/>I'm going to study the test parameter to know more about it.<br/>Thanks a lot for your help and time,I'm very noob with this and that helps me to understand the code.<br/>Thanks. – trugulum Jun 10 '14 at 12:57
  • @user331968 Glad I was able to help. Please consider upvoting and accepting this answer if you found it resolved your problem. Upvoting useful answers and accepting the one that helped you the most to resolve your particular issue is the customary way of saying thank you on the Stack Exchange network, and an accepted answer on a question indicates to the community that you feel the issue has been resolved to your satisfaction. – a CVn Jun 10 '14 at 13:00
  • Sorry but this is my first post here and I haven't enought reputation for upvoting.Thanks again,is working perfectly right now. – trugulum Jun 10 '14 at 16:00
  • @user331968 Keep making constructive contributions and you will soon earn that reputation. I too started out with 1 rep. Welcome to Stack Exchange and Super User. – a CVn Jun 10 '14 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.