puting arguments into varables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting puting arguments into varables
# 1  
Old 05-17-2008
puting arguments into varables

hello all
I am written a script that would put a line of text and a file into two different varables for later use, i.e a script called pimp with any number of words and a filename which is the last argument.

so for eg $pimp my name is the name given to me when i was born Afile
where pimp is the script name
"my name ...... born" is the line of text
and Afile is a name of the file.


I can get the filename since is the last argument but how can i get the remaining argument


this is what i have done.
for arg in "$@"
do
: # nothing
done
echo "the last command line argument is $arg"
# 2  
Old 05-17-2008
The conventional solution would be to put any required arguments first, and leave variable arguments last.

Code:
#!/bin/sh
file=$1
shift
echo "file is $file, remaining arguments are" "$@"

The index of the last command line argument is in $#, and you can use eval to print it.

Code:
eval echo Last argument is ${$#}

eval is tricky to learn, and you need to understand proper quoting rules etc. to use it.
# 3  
Old 05-17-2008
i quiet understand but the $1 would not show the file name it would be part of the word.
i want to capture the last argument and at the same time able to use the rest of the arguments.
so
pimp my name is joe afile
where pimp is the name of the script
my name is joe = a text
afile = name of a file that exist in my home directory
so jow do i capture the afile and d same time able to use my name is joe later
# 4  
Old 05-17-2008
An example how to shift the parameters:

Code:
#!/bin/sh

n=$(( $# - 1 ))

for i in `seq 1 $n`
do
  s=$s" $i"
  shift
done

echo Line: "$s"
echo Name: "$1"

And one with sed:

Code:
#!/bin/sh

line=$(echo "$*"|sed 's/\(.*\) .*/\1/')
name=$(echo "$*"|sed 's/.* \(.*\)/\1/')

echo Line " "$line"
echo Name " "$name"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Too many arguments

because it gives me this error if? while read linea do #echo "Archivos Entrada: $linea" largo=`awk '{print length($linea)}'` echo "largo : $largo " if ; then #Here's the problem, I take this line and it works echo "a es igual a 1" fi... (3 Replies)
Discussion started by: tricampeon81
3 Replies

2. Ubuntu

Sudo commands without puting in .bashrc

dear all, When I start my laptop, I need to run one command /etc/init.open-afs start and it require sudo privilege. The only solution which occur to me is to put this command in .bashrc. But then the trouble comes as everytime I open any new tab it ask for the sudo password, which is pretty... (5 Replies)
Discussion started by: emily
5 Replies

3. Shell Programming and Scripting

For f in * -- too many arguments

Hi guys, I am trying to make a script that will archive all the folders (except NOARCHIVE folder) and then remove those folders: cd my_specific_folder; for f in *; do if ; then tar czf $f.tar.gz $f && rm -r $f; fi; done && echo "All the folders within my_specific_folder are... (3 Replies)
Discussion started by: emanresu
3 Replies

4. Shell Programming and Scripting

: [: too many arguments in for -f in if

Hi Experts , I have following code if ; then mv path /filename newdirpath echo "K* files moved successfully to newdirpath \n" else echo "K* files DID NOT moved successfully to newdirpath \n" fi I am getting "echo "K* files DID NOT moved successfully to newdirpath \n"... (19 Replies)
Discussion started by: ajaypatil_am
19 Replies

5. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

6. Shell Programming and Scripting

while puting shell variable in mysql command value does not interpolate

port=$(ssh tms6@$x cat /tms6/scripts/start.lc.sh | grep -P '^\/tms6\/bin\/lc' | cut -d' ' -f3 | cut -b 3-6) tpsip=$(ssh tms6@$x cat /tms6/scripts/start.lc.sh | grep -P '^\/tms6\/bin\/lc' | cut -d' ' -f4 | cut -b 9-) # IFS="\n" set -- $port ... (1 Reply)
Discussion started by: rrd1986
1 Replies

7. Shell Programming and Scripting

Help puting background process ID's into an array

I am trying to write a script that runs another script consecutively and records the PID of the called script each time it runs in an array. I put in an echo statement to check the PID, when the script runs no PID is output, and the array seems to be empty. I assume it is problem with my code,... (3 Replies)
Discussion started by: Breakology
3 Replies

8. Shell Programming and Scripting

[: too many arguments

Hi Guys I have this small Bash script - but it fails when I'm trying to run it. ./test.sh: && ; then # date >> /writable/sys/shutdown.log shutdown -h "now" exit fi done (4 Replies)
Discussion started by: tainted2real
4 Replies

9. UNIX for Dummies Questions & Answers

Arguments

Ok so i had to create a file and put some random text into it which i did. THen u make a script which takes 2 arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the first line) of... (1 Reply)
Discussion started by: iago
1 Replies
Login or Register to Ask a Question