更改脚本中的PWD以允许访问文件而不添加前缀完整路径

时间:2016-11-20 13:07:40

标签: bash

我知道我应该可以通过做类似于

的操作来更改bash脚本的当前工作目录
cd `dirname $MYPATH`

但由于某种原因,这不起作用(或者不像我想象的那样)。

#!/bin/bash

WAYPATH="/home/user/articles"
TEST_PATH="/home/user/testing"


# Set working directory of the script to be testing
cd `dirname $TEST_PATH`

for i in $(ls $WAYPATH); do

another_command $i $i.r > $TEST_PATH/htmls/$i.html

done

我的目标是允许bash脚本找到位于TEST_PATH中的文件(其名称与WAY_PATH中的文件匹配),而不必使用完整路径作为前缀(因为another_command)使用传递给它的整个参数它

1 个答案:

答案 0 :(得分:0)

所以这是了解在Stackexchange上阅读它们之后命令的作用。我正在使用

cd `dirname $0`

this answer之后,他们取得了预期的结果

$0

cd $MYPATH 是bash脚本的完整路径,因此需要dirname才能返回没有文件名的路径。

相反,对于任意提供的路径就足以做一个简单的

package com.teamagam.dailyselfie;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

class PictureAdapter extends RecyclerView.Adapter<PictureAdapter.PictureViewHolder> {
    private LayoutInflater mInflater;
    private List<PictureInfo> mPictureInfoList;

    PictureAdapter(Context context, List<PictureInfo> pictureInfoList) {
        mInflater = LayoutInflater.from(context);
        mPictureInfoList = pictureInfoList;
    }

    @Override
    public PictureViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_picture, parent, false);
        return new PictureViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PictureViewHolder pictureViewHolder, int position) {
        PictureInfo pictureInfo = mPictureInfoList.get(position);
        pictureViewHolder.mTextView.setText(pictureInfo.fileName);
        int imageSize = pictureViewHolder.mImageView
                .getContext()
                .getApplicationContext()
                .getResources()
                .getInteger(R.integer.activity_main_thumbnail_size);
        if (null != pictureViewHolder.mLoadPictureTask) {
            pictureViewHolder.mLoadPictureTask.cancel(true);
        }
        pictureViewHolder.mImageView.setVisibility(View.INVISIBLE);
        pictureViewHolder.mLoadPictureTask = new LoadPictureTask(pictureViewHolder.mImageView, imageSize);
        pictureViewHolder.mLoadPictureTask.execute(pictureInfo.path);
    }

    @Override
    public int getItemCount() {
        return mPictureInfoList.size();
    }

    class PictureViewHolder extends RecyclerView.ViewHolder {
        ImageView mImageView;
        TextView mTextView;
        LoadPictureTask mLoadPictureTask;

        PictureViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.ivItemListImage);
            mTextView = (TextView) itemView.findViewById(R.id.tvItemListImage);
        }
    }
}

正如评论中所建议的那样。