checking first argument for tag in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting checking first argument for tag in bash script
# 1  
Old 10-18-2012
checking first argument for tag in bash script

I have a bash script where I pass an argument

Code:
./chris.bash "\argv Test"


I want to detect if the user supplied \argv at the start of the argument
# 2  
Old 10-18-2012
Code:
 echo "$1" | grep -q "^[\\]argv" || echo Invalid argument

# 3  
Old 10-18-2012
Have tried it in my script but is not printing anything. It works on the command line though.

I came up with

Code:
if [[ "$1" =~ ^[\\]argv* ]]; then
   msg=`echo "$1" | awk 'BEGIN {FS="argv"} {print $2}' | sed 's/^ *//g'`     
   # Removes the "\argv" and any leading spaces     
   echo "$msg"   
fi

# 4  
Old 10-18-2012
Quote:
Originally Posted by kristinu
Code:
if [[ "$1" =~ ^[\\]argv* ]]; then
   msg=`echo "$1" | awk 'BEGIN {FS="argv"} {print $2}' | sed 's/^ *//g'`     
   # Removes the "\argv" and any leading spaces     
   echo "$msg"   
fi

Or, in bash and ksh (and perhaps others) ...
Code:
msg=${1/#\\argv*( )}

Depending on how your bash is configured, you may need to enable extended globbing:
Code:
shopt -s extglob

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash argument not expanding in script

I pass an argument to bash as run. The first command in green executes as expected, however the second in blue fails as the $run does not expand. I tried to escape the variable with \ thinking the quotes were making the literal translation and also "${run}" but both did not work to expand the... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Checking has for a command line argument

I would like to search and print a match of the user entered $ARGV. Im terrible with hashes and really dont know where to go from here. The example data file name location phone Bugs holeintheground 5551212 Woody holeinthetree 6661313 Jerry holeinthewall 7771414... (4 Replies)
Discussion started by: sumguy
4 Replies

3. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

4. Shell Programming and Scripting

bash script argument not outputing line -NULL

Hi everybody! I am writing a script which accepts two arguments one of them being a message. All is working except for the message part. That is, to accept text as an argument. I have trouble storing and echoing a line of text. It seems that writing a few words and store them in an... (6 Replies)
Discussion started by: bluetxxth
6 Replies

5. Shell Programming and Scripting

How to use perl to run bash with argument?

Hi All, I want to run a bash script using perl. But they are in the different dir. #! /usr/bin/perl -w use strict; my $root=`pwd`; chomp($root); my $cmd=".$root/testdir/ft_623.sh 3 4 5 6 7"; print $cmd; my @line=`$cmd`; foreach (@line){ print $_; } ft_623.sh (0 Replies)
Discussion started by: Damon sine
0 Replies

6. Shell Programming and Scripting

Using GET, passing argument to bash

Hi guys! So, I use GET ( Simple user agent using LWP library. ) on a remote text file that is then passed to bash and executed. However, I need to pass that bash script a single argument, and so far nothing that I have tried has worked, although I have learned quite a bit about input/output... (5 Replies)
Discussion started by: Rhije
5 Replies

7. Shell Programming and Scripting

Bash interprets $1, $2 of an awk script when used in HERE-TAG ...

For the following code: awk -F":" -v OFS=":" 'NR==FNR{a=$2;next}a{$2=a}1' shadow2 shadow1 > shadow3 I get syntex error as below: awk: NR==FNR{a=;next}a{=a}1 awk: ^ syntax error awk: fatal: invalid subscript expression On investigation, I noticed that when I create a bash... (4 Replies)
Discussion started by: Praveen_218
4 Replies

8. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

9. UNIX for Dummies Questions & Answers

Bash script argument problem

I'm having problems with bash scripts. If a bash script is called with no arguments, I always get "PHIST=!" as the first argument (i.e. this is what $1 equals). Why? Where does this come from, and how can I fix it? Nothing in the bash man pages refer to this mysterious default argument. (2 Replies)
Discussion started by: sszd
2 Replies

10. Shell Programming and Scripting

check if argument is an ip address in bash/sh

Hi all, Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh? Thanks, Marc (3 Replies)
Discussion started by: marcpascual
3 Replies
Login or Register to Ask a Question