Multiple Commands on a Single Line

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multiple Commands on a Single Line
# 1  
Old 12-01-2011
Multiple Commands on a Single Line

Hi There,

I have a cronjob that executes a small script (few lines) that I am certain can be achieved in a single line.

The functional objective is actually really simple;

cmd var1

The '1' in 'var1' is actually derived from date (day of month) but the snag is when working with 1-9 I require the core numeric (eg: 1, 2, etc not 01, 02, etc) meaning I cannot use 'date +%d' however using %e produces a leading whitespace. So I have addressed that the only simple way I could understand;

echo `date +%e` | tr -s " "

How can I get the output from this (eg: 1, 24, 7) onto the back of var to achieve my final goal and do so in a single command line (so I can stuff it into Cron).

PS: I am a newbie but I tried searching first but could not quite what I am after and I am really hoping you can help.

Many thanks in advance!
# 2  
Old 12-01-2011
Try the following->
var=var$(date +%e | tr -d " ")
echo $var

# 3  
Old 12-01-2011
On some shells, you can do ${!VARNAME} to get the value of the name pointed to by VARNAME instead of the value of VARNAME itself. But this only works in certain shells.

In others you can do math or string operations on the day to get rid of the leading zero.

So, what's your system? What's your shell?

---------- Post updated at 09:48 AM ---------- Previous update was at 09:44 AM ----------

If you have BASH or ksh:

$ echo cmd var$((1$(date +%d) - 100))
cmd var1

The trickery with subtracting 100 is necessary because BASH will believe numbers starting with 0 to be octal, not decimal! So we cram a 1 on the front then subtract 100...
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 12-01-2011
I am running Solaris 11 with Bash as the default...

Originally Posted by Corona688
If you have BASH or ksh:

$ echo cmd var$((1$(date +%d) - 100))
cmd var1

Thank you for that, it worked like an absolute charm! Many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issues formatting output of two commands in a single line.

I wish to generate output of two commands in the same line separated by a single white-space. Below is my command and output in the same line. ls -ltr fname1.out | awk '{$2=$4=$5=x; print}' | tr '\n' '\t' | tr -s ' '; cksum<fname1.out | cut -d' ' -f1 Output: -rw-r--r--. root Aug 26 16:57... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. Shell Programming and Scripting

convert single line output to multiple line

Hi all, I have a single line output like below echo $ips want to convert above single line output as below format. Pls advice how to do ? Regards Kannan (6 Replies)
Discussion started by: kamauv234
6 Replies

4. UNIX for Dummies Questions & Answers

Redirecting the multiple commands output to single file

Hi, I am new to shell scripting and have a question. I would like to redirect the output of multple commands to single file, From what I read from the bash manpage and from some searching it seems it cannot be done within the shell except setting up a loop. Is it? I am running all clearcase... (1 Reply)
Discussion started by: saku
1 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. Shell Programming and Scripting

Converting Multiple rows to Single Row using unix commands

Can somebody help me in solving this.. Input data is like 0 A 1 B 2 C 3 D 0 A1 1 B1 2 C1 3 D1 0 A2 1 B2 2 C2 3 D2 Output should be like A B C D A1 B1 C1 D1 A2 B2 C2 D2 (7 Replies)
Discussion started by: Mahantesh Patil
7 Replies

7. Shell Programming and Scripting

Running multiple unix commands in a single script

Hi, I would like to write a script with include more than 6 unix commands. my script like below: echo " script started" ls -ld bdf | grep "rama" tail -10 log.txt ... .. ... now, i want to run above unix commands one by one. example: first the ls -ld command will be... (3 Replies)
Discussion started by: koti_rama
3 Replies

8. HP-UX

Automatic execution of commands in multiple servers using single script.

Hi, I've to do a simple job many times whenever it has been asked, just i've to log in to all of fourtien HP servers and i've to execute ps -fu user > temp cat temp|sendmail "" commands to send the statics of all of 14 servers over the mail to particular user id.. Though logging... (4 Replies)
Discussion started by: vickramshetty
4 Replies

9. Shell Programming and Scripting

Putting multiple sed commands on a single line

Hi, I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following: sed '1,7w file1; 8,$w file2' fileA I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Discussion started by: varelg
1 Replies

10. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies
Login or Register to Ask a Question

Featured Tech Videos