开发者

Bash shell script confusing command line argument $1 vs awk $11

开发者 https://www.devze.com 2023-02-01 07:48 出处:网络
I am working on a shell script that takes a command line argument (line 1). Line 2, I want to output the r开发者_开发技巧esult of awk \'{print $11}\' and store it into CURRENT_TAG,

I am working on a shell script that takes a command line argument (line 1). Line 2, I want to output the r开发者_开发技巧esult of awk '{print $11}' and store it into CURRENT_TAG, however, when it comes across print $11 it automatically tries to plug in the command line argument $1 into that spot. How can I avoid this? Putting a \ before the $ does not work.

Thanks,

WORKING_COPY=$1;
CURRENT_TAG=$(ls -l ~/working/svn/$WORKING_COPY/tags/current | grep "current \-\>" | awk '{print $11}');


From your question it seems that current is a symbolic link (or possibly a directory named current containing a symbolic link also named current) and you are trying to figure out what that link references. You can use readlink for that.

CURRENT_TAG=$(readlink ~/working/svn/$WORKING_COPY/tags/current)

OR

CURRENT_TAG=$(readlink ~/working/svn/$WORKING_COPY/tags/current/current)


A modified version of your script works fine for me. How are you executing your script?

markrose@shuttle:/tmp$ cat test.sh
#!/bin/bash

WORKING_COPY=$1;
CURRENT_TAG=$(ls -l /tmp/$1 | awk '{print $1}');
echo $1
echo $CURRENT_TAG
markrose@shuttle:/tmp$ ./test.sh blog.jpeg 
blog.jpeg
-rw-r--r--
markrose@shuttle:/tmp$ bash ./test.sh blog.jpeg
blog.jpeg
-rw-r--r--

For what it's worth, my ls -l output has only 8 fields, the last being the filename (at least with the filename/path containing no spaces and the LFS environment variable not being set).


As noted, it is a bad idea to use ls that way.

Are you saying that the contents of CURRENT_TAG becomes the same as that of WORKING_COPY with a "1" appended?

Note that escaping > makes it special instead of regular. You should do grep "current ->".

Note that AWK can do the selection that you're using grep for.

awk '/current ->/{print $11}'
0

精彩评论

暂无评论...
验证码 换一张
取 消