- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Parameter checking in scripts
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 10:08 PM
тАО05-01-2006 10:08 PM
I need to write a script which performes some action on files which are 4 weeks old (no parameters specified), unless another timeframe is specified (in parameter $1).
I can't figure out how to check if $1 is specified or not.
Any help appreciated.
Pete.
Solved! Go to Solution.
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 10:16 PM
тАО05-01-2006 10:16 PM
Re: Parameter checking in scripts
you could use the length of $1:
a=`expr length "$1"`
or
if [ -z "$1" ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 10:19 PM
тАО05-01-2006 10:19 PM
Re: Parameter checking in scripts
see:
man ksh(1) Section "Parameter Substitution."
<...>
${parameter:=word} If parameter is not set or is null, set it to
word; then substitute the value of the
parameter. Positional parameters cannot be
assigned in this way.
<...>
my be what you're searching for.
rgds
HGH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 10:20 PM
тАО05-01-2006 10:20 PM
Re: Parameter checking in scripts
One way is simply to check the number of arguments passed to the script:
# cat .mysh
#!/usr/bin/sh
if [ $# -eq 0 ]; then
echo "nothing passed"
else
echo "I passed $@"
fi
# ./mysh 123
I passed 123
# ./mysh
nothing passed
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 10:56 PM
тАО05-01-2006 10:56 PM
Re: Parameter checking in scripts
set your script variable conditionally:
...
timespec=${1:-28}
...
find .. -mtime +$timespec ...
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 11:14 PM
тАО05-01-2006 11:14 PM
Re: Parameter checking in scripts
James's solution seems elegant. But when I run this script with parameters it gives correct output. Running a 2nd time without parameters it gives the same output as the 1st run.
Somehow it seems to conserve the parameters???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 11:17 PM
тАО05-01-2006 11:17 PM
Re: Parameter checking in scripts
$ . script.sh a b c
I passed a b c
$ . script.sh
I passed a b c
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 11:35 PM
тАО05-01-2006 11:35 PM
Re: Parameter checking in scripts
Well, that's quite impossible for the script to retain state between executions. I suspect that your terminal settings are to blame. I suspect that you ran the script with arguments; recalled the command line; and "backspaced" over the arguments. If the backspace truly didn't erase, then you would see the behavior you did.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 11:41 PM
тАО05-01-2006 11:41 PM
Re: Parameter checking in scripts
I didn't recall... Is it the way I call the script?
$ ./script.sh
nothing passed
$ ./script.sh a b c
I passed a b c
$ ./script.sh
nothing passed
$ . script.sh
I passed a b c
$ . script.sh d e f
I passed d e f
$ . script.sh
I passed d e f
This last run not recalled!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-01-2006 11:59 PM
тАО05-01-2006 11:59 PM
Re: Parameter checking in scripts
Yes your are 'sourcing' the script instead of simply running it.
Is that deliberate? (why?)
What shell are you in while executing this?
Did you use the exact script as provided, or did you already augment it?
This does not reproduce for me.
From the Posix shell man page:
"The dot (.) special command, as in . file, reads the entire file before any commands are executed. Therefore, alias and unalias commands in the file will not apply to any functions defined in the file."
Groetjes,
Hein.
- Tags:
- source