Sponsored Content
Top Forums Shell Programming and Scripting puting arguments into varables Post 302196253 by era on Saturday 17th of May 2008 10:51:46 AM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

: [: 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

7. 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

8. 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

9. 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
EVAL(1P)						     POSIX Programmer's Manual							  EVAL(1P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
eval - construct command by concatenating arguments SYNOPSIS
eval [argument ...] DESCRIPTION
The eval utility shall construct a command by concatenating arguments together, separating each with a <space>. The constructed command shall be read and executed by the shell. OPTIONS
None. OPERANDS
See the DESCRIPTION. STDIN
Not used. INPUT FILES
None. ENVIRONMENT VARIABLES
None. ASYNCHRONOUS EVENTS
Default. STDOUT
Not used. STDERR
The standard error shall be used only for diagnostic messages. OUTPUT FILES
None. EXTENDED DESCRIPTION
None. EXIT STATUS
If there are no arguments, or only null arguments, eval shall return a zero exit status; otherwise, it shall return the exit status of the command defined by the string of concatenated arguments separated by <space>s. CONSEQUENCES OF ERRORS
Default. The following sections are informative. APPLICATION USAGE
None. EXAMPLES
foo=10 x=foo y='$'$x echo $y $foo eval y='$'$x echo $y 10 RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
Special Built-In Utilities COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 EVAL(1P)
All times are GMT -4. The time now is 09:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy