-1

This is my first shell script program, so i think this is more likely a silly question for you all.

Overview of problem My need is to write a shell command for installing our Node.js server.

  • Server needs to install 3 crons as dependency. We offer 3 different environment.

Problem So when i try to assign cron(string) to variable then all the star in the string are replaced by filenames in the current working directory.

VAR1="*/1       *       *       *       *      /usr/bin/wget -O /var/tmp/output-folder-path  https://my.server.com:12000/cron/push >> /dev/null 2>&1"

Now, when i do echo $VAR1 then output is like following

*/3 file1.txt file2.txt generated.txt sample.sh file1.txt file2.txt generated.txt sample.sh file1.txt file2.txt generated.txt sample.sh file1.txt file2.txt generated.txt sample.sh /usr/bin/wget -O /var/tmp/output-folder-here https://my.server.com:12000/cron/push >> /dev/null 2>&1

Output of ls command in that directory is

file1.txt  file2.txt  generated.txt  sample.sh
  • 2
    I have a very hard time imagining exactly what you are trying to do here, but you can at least certainly prevent those asterisks from being interpreted as glob patterns by using "$VAR1" instead of $VAR1. – Celada Apr 09 '15 at 06:27
  • @Celada This helped me. Thanks, you can post this as answer – Gaurav Gupta Apr 09 '15 at 06:38

1 Answers1

1

You need to put a backspace in front of the * so it's not used as a wildcard.

VAR1="\*/1       \*       \*       \*       \*      /usr/bin/wget -O /var/tmp/output-folder-path  https://my.server.com:12000/cron/push >> /dev/null 2>&1"

EDIT

I was wrong. The * doesn't escape when you do it like I posted above. Instead use single quotes.

VAR1='*/1       *       *       *       *      /usr/bin/wget -O /var/tmp/output-folder-path  https://my.server.com:12000/cron/push >> /dev/null 2>&1'

Additionally, if you don't want to use single quotes you can quote the variable when you print it.

echo "$VAR1"

On a side note I don't think I would ever embed a cron command in a shell script. I doesn't seem like it would be a good practice and it won't work unless you plan to update crontab with the script.

Jeight
  • 2,603