≡ Menu

shell variable

SSH: Use Remote Bash / KSH source Command Set Variable Locally From a Remote Server

I have setup a master / local server combination using OpenSSH. My local server depends upon master server for a remote sourcing. The remote sourcing should set a large number of variables locally in a bash shell script. The code is:

user@slave $ source ssh user@master "/service/dyn.master.setenv.sh --mode=slave"
echo "$var"

The var is not setting and I am getting various errors. How can I do a remote "source" of a bash script file (from master.example.com) and set a shell variable locally at slave.example.com?
[click to continue…]

Unix / Linux Shell: Parse Text CVS File Separator By Field

I work for a small ISP (Internet Service Provider) and we are using Linux and Unix-like operating system with bash shell. I want to write a shell script to parse the csv file line by line. Then line must be parse again field by field. The sample input file is as follows:


I need to extract each of these example.com,username,groupname,homedir,md5password,permission,secondarygroup fields and passed to the different system utilities. How do I write a shell script to automate this task and use the bash shell to parse a text file?
[click to continue…]

Bash: Get The Last Argument Passed to a Shell Script

I'm writing a bash wrapper script that will pass arguments to the command. I need to find out the last argument if I call the wrapper as follows:

./wrapper -a -b --longarg=foo thisfilename.txt
./wrapper -a -b thisfilename.txt
./wrapper -a --next=true thisfilename.txt

=> $@ is all of them.
=> $0 is script name.
=> $1 is first arg.

I want thisfilename.txt stored in a shell variable called $last. How do I find the last argument passed to a shell script written in bash or ksh under Unix like operating systems?
[click to continue…]

Unix / Linux Shell: Get Third Field Separated by Forward Slash (/) Delimiter

I'm writing a shell script for automation purpose. The output the path of the current working directory is stored in $PWD or it can be obtained using the pwd command. How do I find out find out 3rd field separated by the forward slash (/) delimiter using $PWD under Unix like operating systems?
[click to continue…]

Linux/Unix: Bash Set Shell Variable Command

How do I set or create variables within my shell script or at command line under Linux and Unix like operating systems?
[click to continue…]

Linux / Unix: Sed Substitute Multiple Patterns [ Find & Replace ]

I'm using the date +'%D_%T' to store Unix system date and time in a shell variable called $_now:

_now="$(date +'%D_%T')"
echo $_now


I'd like to replace / and : with _. I'm aware of the following sed command:

sed 's/\//_/g
> s/:/_/g' < <<"$_now"


How do I specify two pattern within the same sed command to replace | and : with _ so that I can get output as 01_20_12_16_10_42?
[click to continue…]

Linux / Unix: Bash Shell Assign Printf Result To Variable

How do I assign printf command result to a shell variable under Unix like operating systems?
[click to continue…]

Bash Shell Count Number of Characters In a String or Word

How do I count and print particular character (say D or digit 7) in a string or variable under Bash UNIX / Linux shell?
[click to continue…]

UNIX: Lynx Browser Set / Change the Default Home Page

How do I change the default home page with Lynx browser under UNIX or Linux operating system? How do I set the startup page to http://google.co.uk with Lynx?
[click to continue…]

HowTo: Get / Print Current Date in Unix / Linux Shell Script

How do I get the current date in Unix or Linux shell scripting and store it into a shell variable? How do I print the current date using Unix shell script?
[click to continue…]