Assigning value to script variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning value to script variable
# 1  
Old 02-18-2011
Assigning value to script variable

I am trying to assign the value returned by wc command to a script variale.

Code:
Code:
FILES_NAME='files_list';
NO_OF_FILES =${wc -l $FILES_NAME}`;

When the above code is run : it throws the error
Code:
${wc -l $FILES_NAME}: The specified substitution is not valid for this command.

what is the problemSmilie

Last edited by Franklin52; 02-18-2011 at 03:01 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-18-2011
Code:
NO_OF_FILES=$(cat $FILES_NAME | wc -l)


Last edited by Franklin52; 02-18-2011 at 04:42 AM.. Reason: adding $ sign
# 3  
Old 02-18-2011
Re:Assigning value to script variable

The below code still giving error:

Code:

FILES_NAME='files_list';
NO_OF_FILES =$(wc -l $FILES_NAME);

Error:
unix_4.sh[6]: NO_OF_FILES: not found.
# 4  
Old 02-18-2011
There should be no space between NO_OF_FILES and =
Also, it should be $(wc -l <"$FILES_NAME")
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-18-2011
Quote:
Originally Posted by Scrutinizer
There should be no space between NO_OF_FILES and =
Also, it should be $(wc -l <"$FILES_NAME")
Should not work if the variable $FILES_NAME contains a list of files.
# 6  
Old 02-18-2011
Quote:
Originally Posted by Franklin52
Code:
NO_OF_FILES=$(cat FILES_NAME | wc -l)

Good point. Your solution works in that case (if we add a $ sign Smilie):
Code:
NO_OF_FILES=$(cat $FILES_NAME | wc -l )

If the variable contains files with spaces:
Code:
( IFS="
"
for i in $FILES_NAME
do 
  cat "$i"
done ) | wc -l


Last edited by Scrutinizer; 02-18-2011 at 04:35 AM..
# 7  
Old 02-18-2011
I am trying to get the absolute pathe of the files listed by the code give below .

Code:
#!/bin/csh
FILES_NAME='files_list';
FILE_NAME_PATTERN='uni*';
ls -l $FILE_NAME_PATTERN;
NO_OF_FILES=$(wc -l <"$FILES_NAME");
echo $FILES_LIST;
mail hiten.r.chauhan@gmail.com<<EOF
`ls -l $FILE_NAME_PATTERN`
No of files named $FILE_NAME_PATTERN = $NO_OF_FILES
EOF
rm -f $FILES_NAME;

This code works fine and sends mail as shown below.
Code:
-rw-r----- 1 hitendra seo 205 Feb 15 12:05 uni_1b.sh
-rwxr-xr-- 1 hitendra seo 183 Feb 17 13:44 unix_1a.sh
-rwxr-xr-- 1 hitendra seo 1133 Feb 17 13:52 unix_2.sh
-rwxr-xr-- 1 hitendra seo 131 Feb 17 17:47 unix_3.sh
-rwxr-xr-- 1 hitendra seo 1 Feb 17 13:40 unix_3_instr
-rwxr-xr-- 1 hitendra seo 284 Feb 18 13:49 unix_4.sh
-rw-r----- 1 hitendra seo 12 Feb 17 17:16 unix_5.sh


The home directory structure path is /home/seo/hitendra and the above files are located at the home directory itself.
I want the complete path of the above listed file.

So instead of
Code:
-rw-r----- 1 hitendra seo 205 Feb 15 12:05 uni_1b.sh

I want
Code:
/home/seo/hitendra/uni_1b.sh


Last edited by hiten.r.chauhan; 02-18-2011 at 06:17 AM.. Reason: not properly formated
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning any number to the variable in cshell script

Hello Guys, I would like to ask you for a favor. Could you please help me how can I assign any number as the parameter to a, from stdin (-c), in the following command line by using the 'switch' in a script? awk '$8>a {print "File name:" $5,$8}' I would also appreciate if you can share any... (1 Reply)
Discussion started by: Padavan
1 Replies

2. Shell Programming and Scripting

Assigning value to a variable

Unable to get the value to a variable. set -x cd $HOME echo "Enter the server name" read a echo $a i=4 j=1 k = ps -ef | awk '/server1/{ print $4 }' | tail -$i | head -$j` echo $k When I do the same in command line it works, however the same does not work when I provide that in the... (1 Reply)
Discussion started by: venkidhadha
1 Replies

3. Solaris

Assigning an expression to a variable in shell script

i am trying to assign the following expression to a variable in Unix shell script and want to use that variable in some other expression. But unable to get the required thing done. Please help with this.... This is the expression which i want to provide as input the variable date '+%y:%m:%d' |... (3 Replies)
Discussion started by: ssk250
3 Replies

4. Shell Programming and Scripting

Assigning variable using script

Hi all, I have to write script to make my usual job easy. Basically it involve reading a output of following pattern: crab: ExitCodes Summary >>>>>>>>> 45 Jobs with Wrapper Exit Code : 0 List of jobs:... (10 Replies)
Discussion started by: emily
10 Replies

5. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

6. Shell Programming and Scripting

Script stops running after assigning empty string for a variable

Hi, This is the first time I see something like this, and I don't why it happens. Please give me some help. I am really appreciate it. Basically I am trying to remove all empty lines of an input.. #!/bin/bash set -e set -x str1=`echo -e "\nhaha" | grep -v ^$` #str2=`echo -e "\n" |... (4 Replies)
Discussion started by: yoyomano
4 Replies

7. Shell Programming and Scripting

Assigning return value of an embedded SQL in a shell script variable

I've a script of the following form calling a simple sql that counts the no of rows as based on some conditions. I want the count returned by the sql to get assigned to the variable sql_ret_val1. However I'm finding that this var is always getting assigned a value of 0. I have verified by executing... (1 Reply)
Discussion started by: MxC
1 Replies

8. Shell Programming and Scripting

ksh help assigning specific values to variable in script

Hi - Help needed. I have an input file that looks something like this, but with a lot more entries: A Customer1 B 4500 C 8000 A Customer2 B 6422 C 8922 I need to be able to print details for each customer on one line per customer. ie. if I could print these to a file and then cat... (3 Replies)
Discussion started by: frustrated1
3 Replies

9. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

10. Shell Programming and Scripting

Assigning a value to variable

Another newbie to Unix scripting Q.. How do you assign a value resulting from a command, such as awk, to a variable. I am currently trying:- $awk '{print $1}' file1 > variable1 with no change to $variable1. The line: $awk '{print $1}' file1 does print the first line of the... (3 Replies)
Discussion started by: sirtrancealot
3 Replies
Login or Register to Ask a Question