Script open file linux

How to open a text file from terminal? [closed]

Questions describing a problem that can’t be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers.

I am new to linux and I have a quick question for opening text files using my terminal. I tried many times to open a text file using commands such as

# Option “-x” is deprecated and might be removed in a later version of gnome-terminal.# # Use “-- ” to terminate the options and put the command line to execute after it.# -- xdg-open Random_File.sh --: command not found 

I thought that I might have permission issue, but all read, write and execute permissions are available for my text document

Welcome to unix stack exchange. I suggest you look into the following commands: cat , nano , vi , and vim

@ArmenMkrtumyan please refer to my above comment as any one of those commands is capable of opening a .sh file, if you want to edit however then cat won’t help you

Random.sh is not a shell script file. Linux does not use extensions to determine file types. It is a text file, possibly containing assorted shell commands. Might be executable, might be empty, but xdg-open should open it as a text file. Unless, of course, you have managed to create a association to execute it.

since you have worked out the solution with my comment I have turned it into a more flushed out answer for other users benefit. If you are satisfied with it please consider accepting it

4 Answers 4

There are a few solutions:

vi and vim are text editors, anything you can do in vi can be done in vim but both have a big learning curve for beginners. Nano is also a text editor but is much more user friendly than the former (disclaimer: personal opinion), this being said it may not be installed on your system by default. Lastly cat just displays the contents of your file to the command line, so you may not edit with this command.

If the goal is to read a text file from the command prompt, and be able to scroll the text, then most *NIX systems have the utilities less or more that can be used

robert@pip2:/tmp$ less exampleText.txt 

If you just want to spew the text to the command line, then try cat

robert@pip2:/tmp$ cat exampleText.txt 

If you want to edit a file, then almost all *NIX systems will have vi available

robert@pip2:/tmp$ vi exampleText.txt 

You might have some misconfiguration. My comment above about extensions is incomplete. Linux does have a system based on analysing the first few bytes of a file («magic» numbers) because many well-defined formats (executable binary, compressed files, database tables) conform to standards.

Читайте также:  Will linux run windows programs

However, some tools (launch menus, and xdg-open included) use additional hints to identify specific file types.

The «file» command says this about the files in my home directory:

Paul--) file * > file.txt Box: Bourne-Again shell script, ASCII text executable D_Recovery: directory Executor_1.txt: UTF-8 Unicode text foo.txt: ASCII text, with escape sequences mbox: ASCII text myEnv: ASCII text One: ASCII text One Two Three: ASCII text Pictures: directory Primes: Bourne-Again shell script, ASCII text executable SqlAwk_ENWL.log: ASCII text SqlAwk_NG.log: ASCII text Templates: directory Three: ASCII text Two: ASCII text UL_hSort.txt: ASCII text wdog: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=9a15a7ca3bb94aed54a7a14fb9a11a2dd87d8baa, not stripped wdog.c: C source, ASCII text Xfers: broken symbolic link to /media/paul/0C6E70246E7008AA/Users/Paul/Downloads 

When I run xdg-open UL_hSort.txt , the command prompt comes straight back, but it launches an independent GUI for an editor called Xed. It has a tab for the file, and if I hover over that it says it has a Mime type of plain text document with UTF-8 encoding.

I can run xdg-open with other files from the list, and it opens them as additional tabs in the same GUI. It even changes their Mime type, and does syntax colouring, if I save or reload the file. If I open a Jpeg, it launches a GUI for Xviewer for that file instead. If I open a .docx file (MS Word), it opens LibreOffice Writer for it. And so on.

Источник

Open file in bash script

I’ve got a bash script accepting several files as input which are mixed with various script’s options, for example:

bristat -p log1.log -m lo2.log log3.log -u 

I created an array where i save all the index where i can find files in the script’s call, so in this case it would be an arrat of 3 elements where

arr_pos[0] = 2 arr_pos[1] = 4 arr_pos[3] = 5 
head: cannot open `2' for reading: No such file or directory 

perhaps you are trying to access the second argument by somehow with double substitution. you can check about shift tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html

@abasu i know the standard method for getting an argument from the script’s call ($X where X is the position of the argument) but this doesn’t work..

2 Answers 2

The problem here is that $ stores the index in which you have the file name, not the file name itself — so you can’t simply head it. The array storing your arguments is given by $@ .

A possible way to access the data you want is:

#! /bin/bash declare -a arr_pos=(2 4 5) echo $:1> 

The expansion $:1> means you’re taking the values ranging from the index $ in the array $@ , to the element of index $ + 1 in the same array $@ .

Another way to do so, as pointed by @flaschenpost, is to eval the index preceded by $ , so that you’d be accessing the array of arguments. Although it works very well, it may be risky depending on who is going to run your script — as they may add commands in the argument line.

Читайте также:  Arch linux systemd установка

Anyway, you may should try to loop through the entire array of arguments by the beginning of the script, hashing the values you find, so that you won’t be in trouble while trying to fetch each value later. You may loop, using a for + case . esac , and store the values in associative arrays.

Источник

How do I open a file from inside a bash script?

Ok, so running gedit myfile.txt works well. But what about opening a file from inside a bash script, using the default desktop app linked to the filetype? I’ve tried below, which works great when run manually in terminal, but when I put it in a bash file, nothing happens:

#!/bin/bash xdg-open "myfile.txt"& 

What should I do instead? Please note that I need the file to stay open after the terminal is closed as well.

Are you sure the command does nothing? I just created a script with the contents you list, and it opens gedit with the file listed (or opens a new tab in an existing session). It returns immediately though, rather than blocking until the program exits.

Yep. I use the full path to the file in my script but removed it here to clarify. No difference with or without quotes. I’ve tried xtg-open on *.txt , *.html and more — just wont work.

@abhishek: There are no cases where quotes are harmful in such situations, and plenty of cases where omitting quotes can cause problems. It’s a good practice to make a habit of always quoting all strings, even when unnecessary, unless you have a specific reason not to.

6 Answers 6

I think your script should work. But you might add something to it to get a little more information:

#!/bin/bash T=`xdg-mime query filetype $1` echo "opening file " $1 " of type " $T "with " `xdg-mime query default $T` xdg-open $1 echo "finished script" 

when running this script (named my_open.sh) in a terminal like this:

my_open.sh path/to/somefile.txt 

I get the following output:

opening file path/to/somefile.txt of type text/plain with gedit.desktop finished script 

which tells me that the path to the file is ok, the mimetype is recognized and the desktopfile which is used to open the file is ok as well. And gedit opens with the file in question.

Now when run on another file:

I get the following output:

opening file path/to/README of type text/x-readme with finished script 

Note the different mimetype and the missing desktop file. Nevertheless, xdg-open opens the default for all text files (gedit).

So, you might want to add something like this to your script and see if you get unexpected output (which you can then add to your question. ).

fine, except for the noisy finished script in the end. I recommend [catb.org/~esr/writings/taoup/html/](The art of Unix Programming), the rule of silence. Btw.: Backticks are deprecated — prefer $(. ) instead, which is nestable.

well, the script is meant to give additional information. The information given by ‘finished script’ is that you do not need nohup or & , but that xdg-open returns and lets you continue.

Читайте также:  Pause bash script linux

true. just thought it would be more obvious that way. actually, the whole script is not needed, because it does not more than xdg-open already does, so this is not a production quality script anyway.

Make a bash script test.sh as:

Then, make the script executable as:

Finally, run the script as:

Thanks for your answer, but unfortunately I need to open GNOME’s default file editor for the file as mentioned above.

Maybe gnome-open instead of xdg-open

You are going in the correct direction. If you want the gui app to stay open when you close the terminal window then you just need to add a nohup at the start of the line.

#!/bin/bash nohup xdg-open "myfile.txt"& 

If the gui app is not opening its probably because you do not have the DISPLAY environment variable set in the shell you are trying to launch it from. Try doing an echo $DISPLAY

The first part of your question

with the command cat you can open a file inside the terminal, if that is what you want (it’s stated in the first part of your question).

to use it you can just type cat FILENAME .

Other information

If you need more commands: Here’s a good list of Commands.

GNOME default editor

If you want to open the file in GNOME’s default application gedit .

to use it, just type gedit FILENAME

@Industrial What are you asking for? explain your question further! you say several different things and I have answered one of them but you say you don’t want to use it.

@Industrial the default text editor in GNOME is gedit aka «text editor» so if you run gedit file.filetype you will open the file as you want!

While I’ not sure what’s really to be accomplished here & based on some recent comments that a single script should open ANY file in default app or to extent the app supports this.

If so then easiest way to do that would be by opening a terminal & going scriptname /path/to/filename or if there are any spaces in path then scriptname ‘/path/to/filename’

cd; mkdir -p bin && gedit ~/bin/openit1 

Use this as a script, you can use any name you wish for script, I’ll use openit1 as example. It’s best when using scripts directly from ~/bin to add a number to name so no conflicts with any existing linux commands

Close out gedit & in the terminal

Restart to add ~/bin to your $PATH

To invoke open a terminal and go

openit1 /path/to/filename or openit1 'path/to/filename' 

IF as orig stated & using the orig. script for one particular file per script & invoking by d. left clicking on the script you just need to choose «Run» instead of «Run in Terminal»

Источник

Оцените статью
Adblock
detector