Output only non-empty arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output only non-empty arguments
# 1  
Old 09-10-2010
Output only non-empty arguments

Hello, I am VERY new to shell scripting here, so please go easy. I have an assignment that requires creating a script using bash shell, outputting all command line arguments that are not empty ones such as " ", and showing total number of arguments. I know how to show the total with $# and all arguments with $*, but is there a way to not output empty arguments? I am trying if statements and each time, the arguments appear as white spaces instead of not showing at all.
# 2  
Old 09-10-2010
Can you please post an example how you call your script with the parameters and how your script itself looks?
When doing so, use [code] and [/code] around the code, data or logs to separate it from descriptive text for better readability and preserving indention etc.
# 3  
Old 09-10-2010
Code:
#!/bin/sh 
if [ "$1" != "" ] then 
    echo "Argument 1: $1" 
     fi 
echo "arguments: [$*]" 
echo "Total number of command line arguments: $#"

Output:
Code:
"" 2 3 4
Argument 1: 
arguments: [ 2 3 4]
Total number of command line arguments: 4

I was just testing here with argument $1 and using random numbers as test arguments. There is the white space there before the 2.
# 4  
Old 09-10-2010
Code:
#!/bin/sh

VAR=$(echo $*| sed 's/  / /g')
echo "arguments: [$VAR]"
echo "Total number of command line arguments: $#"

exit 0

This User Gave Thanks to zaxxon For This Post:
# 5  
Old 09-10-2010
Do you really need to pass a space as an argument? That just does not sound right IMO. There are better ways to handle when arguments are not passed.
# 6  
Old 09-10-2010
That seems to do the trick, zaxxon. Thank you. To frank - yes, it was required as part of the exercise.
# 7  
Old 09-10-2010
For next time, just in case, keep that as a reminder please:

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parsing null or empty output

I am working an some if then statements for a script. I want to be able to check for alpha characters or empty out put then exit out. if ]]; echo "Serial Number Invaild" then exit 3; How do I account if the output is empty or null in this in this statement. Many thanks (6 Replies)
Discussion started by: andysensible
6 Replies

2. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk runs but output is empty

The awk below runs, however the output file is 0 bytes. It is basically matching input files that are 21 - 259 records to a file of 11,137,660 records. Basically, what it does is use the input files of which there are 4 to search and match in a large 11,000,000 record file and output the... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. UNIX for Dummies Questions & Answers

Ps command does not output JVM arguments

Hi, My OS is: SunOS mymachine 5.10 Generic_148888-04 sun4v sparc SUNW,SPARC-Enterprise-T5220 I wish to get as much detailed information regarding a java process like the JVM args etc however my current ps command does not yield any detailed output but just one short line. bash-3.2$ ps -ef... (4 Replies)
Discussion started by: mohtashims
4 Replies

5. Shell Programming and Scripting

Sent output to email and empty folder at same time

Hi all, i want to sent output to email and folder at same time. This is my code : echo "Hello" | mailx -s "${SUBJECT}" "${email_add}" >> ${file} I only can sent output to my email but cannot sent to my empty folder....can i know how to done it? (1 Reply)
Discussion started by: proghack
1 Replies

6. Shell Programming and Scripting

Transfer output to empty file?

Hi all, I want transfer the echo data into file.txt.how? echo " $dir $group " >> ${file.txt} ---------- Post updated at 04:11 PM ---------- Previous update was at 03:10 PM ---------- anybody can help ? i mean in script output like echo " hello" i want transfer that output to file.txt. (4 Replies)
Discussion started by: proghack
4 Replies

7. Shell Programming and Scripting

output arguments from a sqlplus

Hi. I need to output a 4 queries result into another application using result=`sqlplus -s ${3}/${4}@${2} << EOF ... query1 query2 query3 query4 .... echo "$metrics1" and returning those individual values into another app. (query1 and 3compute one value, query 2 and 4 compute 4... (3 Replies)
Discussion started by: shell_zen
3 Replies

8. Shell Programming and Scripting

empty option output error

I have a script (multirun.sh) which launches the program bsim_em.x or bsim_es.x depending on the value entered from the screen: > multirun.sh 1 (executes bsim_em.x) > multirun.sh 2 (executes bsim_es.x) which, simplifying, I do with the following lines in the multirun.sh script: if ... (3 Replies)
Discussion started by: josegr
3 Replies

9. Shell Programming and Scripting

Change output if file is empty

I'm very new to writing scripts, so here is my problem...I have the following code already written (in perl) system "rm file2"; open(FILE2, ">file2"); open(MYINPUTFILE, "file"); while(<MYINPUTFILE>) { my($line) = $_; chomp($line); print file2 "$line\n"; print... (2 Replies)
Discussion started by: ddrew78
2 Replies

10. UNIX for Dummies Questions & Answers

The output file is empty. Please Help

Dear all, Currently I writing a ksh script to perform some sql query. I already pipe results in a output file. But when I checked it, the output file is empty. Below is part of the script that I wrote: ------------------------------------------------------------------------ function... (4 Replies)
Discussion started by: balzzz
4 Replies
Login or Register to Ask a Question