wademi
asked on
Assigning variables Unix
I have a c shell script that has the follwoing variables declared.
set mntcheckcmd = "ls -ltr /home/milligram/mnt |wc -l"
@ mntcheckAkamai = `@mntcheckcmd`
echo mntcheck
@ copycount = 0
@ count = 0
chdir $SourceDir
if($mntcheckAkamai == 1)then
.........
Basically when I run mntcheckcmd it should return an integer which indicates if the directory is mounted.
How would I execute the "ls -ltr /home/milligram/mnt |wc -l" part as a command.
How do I assign this variable?
Use $ instead of @
ASKER
So my variables looks like this:
set mntcheckcmd = "ls -ltr /home/milligram/mnt |wc -l"
@ mntcheckAkamai = `$mntcheckcmd`
echo mntcheckAkamai
Whe I execute my script I get the follwoing error:
milligram@ubuntu:~$ ./mills1.1.csh
ls: cannot access |wc: No such file or directory
`$mntcheckcmdAkamai`: Ambiguous.
when I execute ls -ltr /home/milligram/mnt |wc -l I get back " 6".
The directory actually exsist.
set mntcheckcmd = "ls -ltr /home/milligram/mnt |wc -l"
@ mntcheckAkamai = `$mntcheckcmd`
echo mntcheckAkamai
Whe I execute my script I get the follwoing error:
milligram@ubuntu:~$ ./mills1.1.csh
ls: cannot access |wc: No such file or directory
`$mntcheckcmdAkamai`: Ambiguous.
when I execute ls -ltr /home/milligram/mnt |wc -l I get back " 6".
The directory actually exsist.
Try it like so:
(using back ticks instead of double quotes)
set mntcheckcmd = `ls -ltr /home/milligram/mnt |wc -l`
@ mntcheckAkamai = `$mntcheckcmd`
echo mntcheckAkamai
(using back ticks instead of double quotes)
set mntcheckcmd = `ls -ltr /home/milligram/mnt |wc -l`
@ mntcheckAkamai = `$mntcheckcmd`
echo mntcheckAkamai
ASKER
I am getting the same error
The file actually exsist:
milligram@ubuntu:~$ ls -ltr /home/milligram/mnt |wc -l
6
The file actually exsist:
milligram@ubuntu:~$ ls -ltr /home/milligram/mnt |wc -l
6
ASKER
Here is what I have now
set mntcheckcmdAkamai = 'ls -ltr /home/milligram/mnt |wc -l'
@ mntcheckAkamai = `$mntcheckcmdAkamai`
echo mntcheckAkamai
. I changed the name of the variable
set mntcheckcmdAkamai = 'ls -ltr /home/milligram/mnt |wc -l'
@ mntcheckAkamai = `$mntcheckcmdAkamai`
echo mntcheckAkamai
. I changed the name of the variable
You need to give the full path of wc command
ASKER
Why would the wc command work from the prompt and not from the script?
Sorry I am kinda new to linux.
Sorry I am kinda new to linux.
Getting that pipe to work in c shell seems to be a royal pain in the gluteus maximus!
I just tried it and it doesn't work with those set commands.
I had to use setenv instead of set and keep the pipe out until the command is run, as follows:
#set the first part of the command
setenv mntcheckcmd1 "ls -ltr /home/milligram/mnt"
#set the first part of the command
setenv mntcheckcmd2 "wc -l"
# run both parts with the pipe here and put the output into mntcheck
setenv mntcheck `$mntcheckcmd1 | $mntcheckcmd2`
echo $mntcheck
I don't know why it doesn't work to put the pipe in the variable - it just doesn't for me when testing on RHEL 5. Things don't seem to work properly in csh if I use set instead of setenv either.
It's been a long time since I used csh - now I remember why I switched to bash and ksh !
I just tried it and it doesn't work with those set commands.
I had to use setenv instead of set and keep the pipe out until the command is run, as follows:
#set the first part of the command
setenv mntcheckcmd1 "ls -ltr /home/milligram/mnt"
#set the first part of the command
setenv mntcheckcmd2 "wc -l"
# run both parts with the pipe here and put the output into mntcheck
setenv mntcheck `$mntcheckcmd1 | $mntcheckcmd2`
echo $mntcheck
I don't know why it doesn't work to put the pipe in the variable - it just doesn't for me when testing on RHEL 5. Things don't seem to work properly in csh if I use set instead of setenv either.
It's been a long time since I used csh - now I remember why I switched to bash and ksh !
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Note - if you are testing at the command prompt I suggest typing "unset mntcheck" first or the "setenv" command won't work as you have previously "set" the variable!
Yes, it only seems to work if the pipe is in the final command which is being run and not passed in as a variable to that command. I hope that makes sense ...
>> Why would the wc command work from the prompt and not from the script?
When you run a script, it runs in a sub-shell. It may not get seeing the env variables. To test this, do this:
Issue command
csh
ls
And then you can exit
When you run a script, it runs in a sub-shell. It may not get seeing the env variables. To test this, do this:
Issue command
csh
ls
And then you can exit
You will see the environment variables if you set them with setenv rather than set. That's like using export in bash or ksh. That's not the problem I'm seeing. It's passing the pipe character in to another command by environment variable that doesn't seem to work. I don't understand why that should be, but that's the bit that's not working for me.
ASKER
farzanj:
@ mntcheckAkamai = 'ls -ltr /home/milligram/mnt |wc -l'
echo "@mntcheckAkamai"
This gives errors:
milligram@ubuntu:~$ ./mills1.1.csh
@: Expression Syntax.
@ mntcheckAkamai = 'ls -ltr /home/milligram/mnt |wc -l'
echo "@mntcheckAkamai"
This gives errors:
milligram@ubuntu:~$ ./mills1.1.csh
@: Expression Syntax.
ASKER
Martin_J_Parker:
setenv mntcheckcmdAkamai1 = "ls -ltr /home/milligram/mnt"
setenv mntcheckcmdAkamai12 = "wc -l"
setenv mntcheckAkamai `$mntcheckcmdAkamai1 | $mntcheckcmdAkamai12`
echo $mntcheckAkamai
This gives errors:
milligram@ubuntu:~$ ./mills1.1.csh
setenv: Too many arguments.
setenv mntcheckcmdAkamai1 = "ls -ltr /home/milligram/mnt"
setenv mntcheckcmdAkamai12 = "wc -l"
setenv mntcheckAkamai `$mntcheckcmdAkamai1 | $mntcheckcmdAkamai12`
echo $mntcheckAkamai
This gives errors:
milligram@ubuntu:~$ ./mills1.1.csh
setenv: Too many arguments.
Of course it would. But you are using single quotes, I am using back ticks.
' ' vs ` `
' ' vs ` `
Probably same problem with mine - use back ticks not forward quotes on that last command.
The back tick is on the same key as | on my UK keyboard whereas the quote is on the @ key!
The back tick is on the same key as | on my UK keyboard whereas the quote is on the @ key!
Well done farzanj - but I'd still like to nkow why passing a pipe via an environment variable doesn't work! :-)
I actually gave you the correct answer close to the top of this thread.
Your mistake was you were using quotes instead of back ticks. Back ticks are below the tilde on a US keyboard.
Your mistake was you were using quotes instead of back ticks. Back ticks are below the tilde on a US keyboard.
@ mntcheckAkamai = `$mntcheckcmd`
echo mntcheck
@ copycount = 0
@ count = 0