Determine a shell variable exists in file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine a shell variable exists in file?
# 1  
Old 07-16-2013
Determine a shell variable exists in file?

Hi Folks,

Trying to build up a script that will lookup a username invoked as:

Code:
./buildscript.sh <username>

This should take <username> and look it up in <username_file> and prepare for further processing. Here is the snippet that isn't working just right:

Code:
user=$1
if [[ !{$user = '{grep $user username_file}' -eq 1 ]]; then
  echo usage: $0 username
  echo "  where username is your domain username"
  exit 1
fi

Please be gentle, my script-fu is weak.
# 2  
Old 07-16-2013
You could do something like:
Code:
#!/bin/bash

if [ $# -ne 1 ]
then
        echo "Usage: $0 <username>"
        exit 1
else
        user="$1"
        if grep -q "$user" username_file
        then
                echo "User: $user present"
        else
                echo "User: $user not present"
        fi
fi

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell scripting to determine special chars in file

Hi, I need all your help to achieve the below functionality. I have a big 2 GB file and inside the file we need to identify, whether having a comma(,) or pipe(|) or tab or fixed position or semicolon(;) delimiter. If any of those delimiter found need to replace the file with pipe(|)... (1 Reply)
Discussion started by: lkeswar
1 Replies

2. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

3. Shell Programming and Scripting

If a string variable value exists in a set of values

Can some one please help me with the syntax in shell script for the below : if $var1 exists in ('val1','val2','val3') I want to execute a set of commands if the value of var1 variable matches any one of the given string values. Please let me know if there are any other option to go by. ... (10 Replies)
Discussion started by: Pandee
10 Replies

4. Shell Programming and Scripting

Determine if variable is the Server component of UNC

Hi all, Using sh/csh, unfortunately shell scripts are not my strong suit. Trying to write a script that gets called from a program for pre-processing. The program passes individual components of a UNC (//server/path1/path2/filename). Thus the unc path of: //server/path1/path2/filename, is... (7 Replies)
Discussion started by: Festus Hagen
7 Replies

5. Shell Programming and Scripting

clause for setting a variable to $1 if it exists

Hi all, What is the simplest way to setting a variable to $1 if it exists ? If I go with name=${"$1":-abc}, bash complains "bad substitution", So I use name="$1" name=${name:-abc} But is there a way to fix this "bad substitution" ? Thanks! (2 Replies)
Discussion started by: qiulang
2 Replies

6. Shell Programming and Scripting

Checking if file exists using a NOT operator and shell variable

Hi, I'm new to UNIX, at least shell programming and am having trouble figuring out a problem i'm having. In one section in my nested if statement, i want the program to test if the file does not exist, based on an argument supplied at the command line by the user. What i have is elif ; then... (3 Replies)
Discussion started by: rowlf
3 Replies

7. Shell Programming and Scripting

Shell script to check if any file exists in 4 folders

Hi All, working on AIX 5.3. Requirement is: Shell script in ksh to check if any file exists in 4 folders as below: 1. /FILE/INB/INT1 2. /FILE/INB/INT2 3. /FILE/INB/INT3 4. /FILE/INB/INT4 Thanks a lot for your time! a1_win. (3 Replies)
Discussion started by: a1_win
3 Replies

8. Shell Programming and Scripting

Chek if a file exists in Ubuntu and Cent OS using shell script

I have tried few examples in the internet but all of them are different and none worked. I need to check if a file exists in a directory if it does not then exit . here is what I have for now $filename ="/usr/local/net/var/lib/directoryservice/sync.disable" if ; then echo "The file exists"... (2 Replies)
Discussion started by: m_kk
2 Replies

9. Shell Programming and Scripting

Determine date and time the file was created through shell scripts

Can I determine when the particular file was created, in korn-shell. Can please someone help me. If possible please mail the solution to me. my mail id: bharat.surana@gmail.com (1 Reply)
Discussion started by: BharatSurana
1 Replies

10. UNIX for Dummies Questions & Answers

If file exists (bourne shell)

Im trying to code some logic into a test script to test for the existence of a file before recreating it. Im using the following line to test for this: if -r test.txt; However I get the error message ./testScript.sh: -r: not found Having read through the man pages im still not clear whats... (2 Replies)
Discussion started by: blakmk
2 Replies
Login or Register to Ask a Question